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 ?
Asked
Active
Viewed 1,798 times
-1
-
Python 4.7? Is this the number of an exercise? Surely not the language version ... – phynfo Nov 15 '17 at 20:55
-
3Python 4.7? You may be asking from the future. However, we do not do homeworks. – Elis Byberi Nov 15 '17 at 20:56
-
1Just use the `count`-method from class `list`: `[1,1,2,1].count(1)` for example – phynfo Nov 15 '17 at 20:56
-
3Do not be too harsh. First time asking a question. Give him some advice. – Elis Byberi Nov 15 '17 at 20:57
-
Answered and someone please edit the title and write 2.7 – alexisdevarennes Nov 15 '17 at 21:01
-
1@alexisdevarennes Leave the title as it is. Editing it will make all these comments off-topic. – Elis Byberi Nov 15 '17 at 21:14
2 Answers
6
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})

Robbie Milejczak
- 5,664
- 3
- 32
- 65
-
-
2Would upvote, but out of points. Still leaving my answer so he gets the logic behind it. – alexisdevarennes Nov 15 '17 at 21:02
-
yours is better in the case that a human readable result is needed anyway :) – Robbie Milejczak Nov 15 '17 at 21:04
4
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:

alexisdevarennes
- 5,437
- 4
- 24
- 38
-
2Calling `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. – user2357112 Nov 15 '17 at 21:06
-
Catastrophically means its time complexity in O(n) where n is length of list. – Elis Byberi Nov 15 '17 at 21:09
-
1No, 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 Nov 15 '17 at 21:34
-
@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. – Neil Nov 15 '17 at 21:40
-
@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. – Elis Byberi Nov 15 '17 at 21:41
-
@alexisdevarennes Here is a similar version with a time complexity of O(n): https://pastebin.com/LiP93Buj . Note: `item in di` is O(1). – Neil Nov 15 '17 at 21:45
-
-
-
-
You may want to automatically generate a much larger list of random numbers to get a more accurate read. But, anyway, good answer, upvoted. – Neil Nov 15 '17 at 22:01
-