在 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)
谁能帮帮我
请您参考如下方法:
您可以使用集合中的计数器。
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]
