I am fairly new to python, but I haven't been able to find a solution to my problem anywhere.
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
for entry in list1:
print list1.count(entry[0])
As the same ID occurs twice in the list, this should return:
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.
*EDIT: Using Eumiro's awesome answer. 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:
for name, value in list1:
if value > 1:
print value
I get this error:
ValueError: Too many values to unpack
In the above example, we have used the count() method to count the number of lists and tuples inside the tuple random . The tuple ('a', 'b') appears twice and the list [3, 4] appears once. Hence, its count in the tuple is 2 and 1 respectively.
Python Tuple count() Method Python count() method counts the occurrence of an element in the tuple. It returns the occurrence of the the element passed during call. It required a parameter which is to be counted. It returns error if the parameter is missing.
Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.
The easiest way to count the number of occurrences in a Python list of a given item is to use the Python . count() method. The method is applied to a given list and takes a single argument. The argument passed into the method is counted and the number of occurrences of that item in the list is returned.
Maybe collections.Counter
could solve your problem:
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:
for entry in list1:
print(sum(1 for elem in list1 if elem[0] == entry[0]))
But seriously, have a look at collections.Counter
.
EDIT: I actually need the total amount of entries which has a value more than 1.
You can still use the Counter
:
c = Counter(elem[0] for elem in list1)
sum(v for k, v in c.iteritems() if v > 1)
returns 2
, i.e. the sum of counts that are higher than 1.
list1.count(entry[0])
will not work because it looks at each of the three tuples in list1
, eg. ('12392', 'some string', 'some other string')
and checks if they are equal to '12392'
for example, which is obviously not the case.
@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
):
>>> 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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With