在Python中,我有一个数组,
("192.168.1.1","high"),("192.168.1.1","high"),("192.168.1.1","low"),("192.168.1.1","low"),("192.168.1.2","high"),("192.168.1.2","medium")
我需要输出显示计数
("192.168.1.1","high",2),("192.168.1.1","low",2),("192.168.1.2","high",1),("192.168.1.2","medium",1)
任何人都请帮助我
答案 0 :(得分:3)
您可以使用收藏品中的计数器。
from collections import Counter
l = [("192.168.1.1","high"),("192.168.1.1","high"),("192.168.1.1","low"),("192.168.1.1","low"),("192.168.1.2","high"),("192.168.1.2","medium")]
counter = Counter(l)
result = [(*key, counter[key]) for key in counter]
答案 1 :(得分:1)
如果您不关心订单:
l = [("192.168.1.1","high"),("192.168.1.1","high"),("192.168.1.1","low"),("192.168.1.1","low"),("192.168.1.2","high"),("192.168.1.2","medium")]
list(set([(*t, l.count(t)) for t in l]))