Basically I have a list with 250 elements, some of them are 1 some 2 and some 3. How do I get a variable that represents the number of ‘1’s ?
2 Answers
This exists natively in Python
from collections import Counter
A = [1,1,1,1,1,2,2,2,3,3,3,3,3,4,4,4,4]
B = Counter(A)
outputs Counter({1: 5, 3: 5, 4: 4, 2: 3})
3 Comments
alexisdevarennes
Nice way to do it!
alexisdevarennes
Would upvote, but out of points. Still leaving my answer so he gets the logic behind it.
Robbie Milejczak
yours is better in the case that a human readable result is needed anyway :)
I assume this will still be the case in python 4.7
Jokes aside, this will work in Python 2.7 (and 3 etc)
Using count():
your_list = [1,1,2,3,4,5,6,7,8]
distinct_values = set(your_list)
if len(distinct_values) == len(your_list)
print('All values have a tf count of 1')
else:
for distinct_value in distinct_values:
print('%s n occurences: %s' % (distinct_value, str(your_list.count(distinct_value))))
Using in (Solution provided by @nfn neil via comments):
your_list = [1,1,2,3,4,5,6,7,8]
di = {}
for item in your_list:
if item in di:
di[item] += 1
else:
di[item] = 1
print(di)
benchmarks running both variants 300 times with a list of 500 000 items:
The runtime for using_in took 18.3120000362 seconds to complete
The runtime for using_count took 743.9941547110 seconds to complete
Benchmark code:
11 Comments
user2357112
Calling
count in a loop requires a separate pass over the input for every count call. It's catastrophically slow for inputs with a lot of distinct items.Elis Byberi
Catastrophically means its time complexity in O(n) where n is length of list.
Neil
No, I'm pretty sure this would be O(n^2) worst case scenario. Let's say we have a list of unique values, then we are looping through each value in the
list. Then we have to loop (again) over each item to get the count of the item for every item in the list.Neil
@alexisdevarennes While I do appreciate the gesture, it doesn't fix the underlying issue. What if every value is unique in the list, except there is 1 duplicate. Still it would have to iterate over it in an n^2 manner.
Elis Byberi
@nfnneil I was talking about the
count() method time complexity. I forgot to write count(). You are right. else block time complexity is O(n^2) in the worst case scenario. |
count-method from classlist:[1,1,2,1].count(1)for example