[英]Counting the amount of occurrences in a list of tuples
I am fairly new to python, but I haven't been able to find a solution to my problem anywhere. 我是python的新手,但我无法在任何地方找到解决问题的方法。
I want to count the occurrences of a string inside a list of tuples. 我想计算元组列表中字符串的出现次数。
Here is the list of tuples: 这是元组列表:
list1 = [
('12392', 'some string', 'some other string'),
('12392', 'some new string', 'some other string'),
('7862', None, 'some other string')
]
I've tried this but it just prints 0 我试过这个,但它只打印0
for entry in list1:
print list1.count(entry[0])
As the same ID occurs twice in the list, this should return: 由于相同的ID在列表中出现两次,因此应该返回:
2
1
I also tried to increment a counter for each occurrence of the same ID but couldn't quite grasp how to write it. 我也尝试为每次出现相同ID的计数器增加一个计数器,但却无法完全掌握如何编写它。
*EDIT: Using Eumiro's awesome answer. *编辑:使用Eumiro的精彩答案。 I just realized that I didn't explain the whole problem. 我才意识到我没有解释整个问题。 I actually need the total amount of entries which has a value more than 1. But if I try doing: 我实际上需要具有大于1的值的条目总数。但是如果我尝试这样做:
for name, value in list1:
if value > 1:
print value
I get this error: 我收到此错误:
ValueError: Too many values to unpack
Maybe collections.Counter
could solve your problem: 也许collections.Counter
可以解决你的问题:
from collections import Counter
Counter(elem[0] for elem in list1)
returns 回报
Counter({'12392': 2, '7862': 1})
It is fast since it iterates over your list just once. 它很快,因为它只在你的列表上迭代一次。 You iterate over entries and then try to get a count of these entries within your list. 您迭代条目,然后尝试在列表中计算这些条目。 That cannot be done with .count
, but might be done as follows: 使用.count
无法做到这一点,但可能会按如下方式完成:
for entry in list1:
print sum(1 for elem in list1 if elem[0] == entry[0])
But seriously, have a look at collections.Counter
. 但说真的,看看collections.Counter
。 collections.Counter
。
EDIT : I actually need the total amount of entries which has a value more than 1. 编辑 : 我实际上需要值大于1的条目总数。
You can still use the Counter
: 你仍然可以使用Counter
:
c = Counter(elem[0] for elem in list1)
sum(v for k, v in c.iteritems() if v > 1)
returns 2
, ie the sum of counts that are higher than 1. 返回2
,即高于1的计数总和。
list1.count(entry[0])
will not work because it looks at each of the three tuples in list1
, eg. list1.count(entry[0])
将不起作用,因为它查看list1
中的三个元组中的每一个,例如。 ('12392', 'some string', 'some other string')
and checks if they are equal to '12392'
for example, which is obviously not the case. ('12392', 'some string', 'some other string')
并检查它们是否等于'12392'
,这显然不是这种情况。
@eurmiro's answer shows you how to do it with Counter
(which is the best way!) but here is a poor man's version to illustrate how Counter
works using a dictionary and the dict.get(k, [,d])
method which will attempt to get a key ( k
), but if it doesn't exist it returns the default value instead ( d
): @ eurmiro的答案向您展示了如何使用Counter
(这是最好的方法!)但是这里有一个穷人的版本来说明Counter
如何使用字典和dict.get(k, [,d])
方法工作尝试获取密钥( k
),但如果它不存在则返回默认值( d
):
>>> list1 = [
('12392', 'some string', 'some other string'),
('12392', 'some new string', 'some other string'),
('7862', None, 'some other string')
]
>>> d = {}
>>> for x, y, z in list1:
d[x] = d.get(x, 0) + 1
>>> d
{'12392': 2, '7862': 1}
I needed some extra functionality that Counter didn't have. 我需要一些Counter没有的额外功能。 I have a list of tuples that the first element is the key and the second element is the amount to add. 我有一个元组列表,第一个元素是键,第二个元素是要添加的元素。 @jamylak solution was a great adaptation for this! @jamylak解决方案很适合这个!
>>> list = [(0,5), (3,2), (2,1), (0,2), (3,4)]
>>> d = {}
>>> for x, y in list1:
d[x] = d.get(x, 0) + y
>>> d
{0: 7, 2: 1, 3: 6}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.