-1

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 Answers2

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
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:

https://pastebin.com/dWS8UH7c

alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
  • 2
    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. – 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
  • 1
    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 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
  • @nfnneil I added your version, I hope that is OK – alexisdevarennes Nov 15 '17 at 21:57
  • I also did some benchmarks – alexisdevarennes Nov 15 '17 at 21:57
  • https://pastebin.com/S8X2g2tm – alexisdevarennes Nov 15 '17 at 21:57
  • 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
  • https://pastebin.com/dWS8UH7c – alexisdevarennes Nov 15 '17 at 22:06