以下是一个示例函数,可以按照每天迭代列中时间戳出现的次数:
from collections import Counter
def count_timestamps_per_day(timestamps):
# 将时间戳转换为日期字符串,并计算每个日期出现的次数
date_counts = Counter([timestamp.split()[0] for timestamp in timestamps])
# 返回日期和对应的次数
return date_counts
使用示例:
timestamps = ['2022-01-01 10:30:45', '2022-01-01 12:15:30', '2022-01-02 14:20:00', '2022-01-03 08:45:15']
timestamp_counts = count_timestamps_per_day(timestamps)
print(timestamp_counts)
输出:
Counter({'2022-01-01': 2, '2022-01-02': 1, '2022-01-03': 1})
以上函数使用了collections
模块中的Counter
类来计算每个日期出现的次数。函数接受一个时间戳列表作为参数,遍历列表中的每个时间戳,取日期部分并统计出现的次数。最后返回一个包含日期和对应次数的Counter
对象。
下一篇:按照每天特定小时间隔进行分组