Python 如何按时间对项目(从文件中读取)进行分组和计数?

Python 如何按时间对项目(从文件中读取)进行分组和计数?,python,list,count,tuples,Python,List,Count,Tuples,我试图从一个文件中获取输入,其中一行包含两个字段: (时间、状态) 使用Python,我将输入文件转换为列表中的以下格式: [('13:00', 'HELLO'), ('16:00', 'TESTING'), ('18:00', 'BYE'), ('15:00', 'BYE'), ('18:00', 'HELLO'), ('13:00', 'BYE'), ('13:00', 'HELLO')] 因此,我可以使用集合中的计数器来计算状态出现的次数,给出以下信息: Counter({('13:00

我试图从一个文件中获取输入,其中一行包含两个字段: (时间、状态)

使用Python,我将输入文件转换为列表中的以下格式:

[('13:00', 'HELLO'), ('16:00', 'TESTING'), ('18:00', 'BYE'), ('15:00', 'BYE'), ('18:00', 'HELLO'), ('13:00', 'BYE'), ('13:00', 'HELLO')]
因此,我可以使用集合中的计数器来计算状态出现的次数,给出以下信息:

Counter({('13:00', 'HELLO'): 2, ('16:00', 'TESTING'): 1, ('18:00', 'BYE'): 1, ('15:00', 'BYE'): 1, ('18:00', 'HELLO'): 1, ('13:00', 'BYE'): 1})
我想做的是计算状态出现的次数,然后根据时间对其进行分组,以便最终输出为:(时间、HELLO计数、BYE计数、测试计数)

然而,我不知道如何把它转换成那种格式

我试图使用列表理解,但不明白如何一次将每个状态的计数器传递到列表中

有什么建议吗?

你可以用of来计算物品的数量

它首先将
计数器
对象分组为类似
{'13:00':计数器({'HELLO':2,'BYE':1}),'16:00':计数器({'TESTING':1})
,然后将结果解压到(TIME,HELLO COUNT,BYE COUNT,TESTING COUNT)元组中

输出:

[('13:00', 2, 1, 0), ('16:00', 0, 0, 1), ('18:00', 1, 1, 0), ('15:00', 0, 1, 0)]

下面的代码可以使用

items = [('13:00', 'HELLO'), ('16:00', 'TESTING'), ('18:00', 'BYE'), ('15:00', 'BYE'), ('18:00', 'HELLO'), ('13:00', 'BYE'), ('13:00', 'HELLO')]
data = {}
indexs = {"HELLO": 0, "BYE": 1, "TESTING": 2}
for item in items:
     if item[0] not in data:
         data[item[0]] = [0,0,0]
     data[item[0]][indexs[item[1]]] += 1
print([tuple([k]+v) for k, v in data.items()])

它需要使用元组列表吗?我会在操作时间戳的同时开始填充dict。但是如果你需要元组列表,它是不同的。不需要以元组列表开始,它只是帮助我找到Stuck。我给出了这个答案,不仅因为它有效,而且我理解它的原理它起作用了。
from collections import Counter
from collections import defaultdict

d = Counter({('13:00', 'HELLO'): 2, ('16:00', 'TESTING'): 1, ('18:00', 'BYE'): 1, ('15:00', 'BYE'): 1, ('18:00', 'HELLO'): 1, ('13:00', 'BYE'): 1})

count_keys = ['HELLO', 'BYE', 'TESTING']

# count and group 
counts = defaultdict(Counter)
for (time, count), testing in d.items():
    counts[time][count] += testing

# output and unpack items from count dictionary
print([(k1, *(v1[k2] for k2 in count_keys)) for k1, v1 in counts.items()])
[('13:00', 2, 1, 0), ('16:00', 0, 0, 1), ('18:00', 1, 1, 0), ('15:00', 0, 1, 0)]
items = [('13:00', 'HELLO'), ('16:00', 'TESTING'), ('18:00', 'BYE'), ('15:00', 'BYE'), ('18:00', 'HELLO'), ('13:00', 'BYE'), ('13:00', 'HELLO')]
data = {}
indexs = {"HELLO": 0, "BYE": 1, "TESTING": 2}
for item in items:
     if item[0] not in data:
         data[item[0]] = [0,0,0]
     data[item[0]][indexs[item[1]]] += 1
print([tuple([k]+v) for k, v in data.items()])