0

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 ?

6
  • Python 4.7? Is this the number of an exercise? Surely not the language version ... Commented Nov 15, 2017 at 20:55
  • 3
    Python 4.7? You may be asking from the future. However, we do not do homeworks. Commented Nov 15, 2017 at 20:56
  • 1
    Just use the count-method from class list: [1,1,2,1].count(1) for example Commented Nov 15, 2017 at 20:56
  • 3
    Do not be too harsh. First time asking a question. Give him some advice. Commented Nov 15, 2017 at 20:57
  • 1
    @alexisdevarennes Leave the title as it is. Editing it will make all these comments off-topic. Commented Nov 15, 2017 at 21:14

2 Answers 2

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})

Sign up to request clarification or add additional context in comments.

3 Comments

Nice way to do it!
Would upvote, but out of points. Still leaving my answer so he gets the logic behind it.
yours is better in the case that a human readable result is needed anyway :)
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

11 Comments

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.
Catastrophically means its time complexity in O(n) where n is length of list.
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.
@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.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.