Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python数组组计数_Python - Fatal编程技术网

Python数组组计数

Python数组组计数,python,Python,在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.16

在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)

如果您不关心订单,请任何人帮助我:

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]))

如果您不关心订单:

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]))

您可以使用集合中的计数器

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]

您可以使用集合中的计数器

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]

[*t,数组中t的借词]您能展示一下您的尝试吗?@ChristianDean它会为每个项目的出现添加每个元素,例如192.168.1.1,高,2它会出现两次。回答注意:请停止回答这个问题。像这样的问题涉及面很广,应该就此结束,而不是回答。有关更多详细信息,请参见。[*t,数组中的t的借词]您可以显示您尝试过的内容吗?@ChristianDean它将为每个项目的出现添加每个元素,例如,对于192.168.1.1,高,2,它将出现两次。回答注意:请停止回答此问题。像这样的问题涉及面很广,应该就此结束,而不是回答。有关更多详细信息,请参阅。