-2
alist = [5, 7, 6, 2, 9, 1, 7]

D = {}

for each unique number in list, set a new key for each key in dictionary, count number of that key and set to value

this should look like {5:1, 2:1, 6:1, 9:1, 1:1, 7:2}

algorithm:

For each number n on the input list:
∗ If n in count: set count[n] to count[n] + 1
∗ else: set count[n] to 1

I don't know how to go about this. Can anybody show me how?

Attempt:

for number in alist:
    if number in D:
        D[number] = D[number]+1
    else:
        D[number] = 1

Error:

Traceback (most recent call last): File "<pyshell#15>", line 3, in
<module> D[number] = D[number]+1 KeyError: 5
wwii
  • 23,232
  • 7
  • 37
  • 77
Brice Petty
  • 17
  • 1
  • 4
  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Morgan Thrapp Nov 24 '15 at 19:12
  • I'm new to this and I'm trying. I tried writing it out but I get an error. this is what I'm at: for number in alist: if number in alist: D[number] = D[number+1] else: D[number] = 1 – Brice Petty Nov 24 '15 at 19:15
  • move the `+1` in `D[number+1]` to outside of the brackets like `D[number]+1` or simply change that line to `D[number]+=1` – R Nar Nov 24 '15 at 19:17
  • You should post the Traceback with your question when you are getting an error as well as the code for your attempt to solve the problem. – wwii Nov 24 '15 at 19:20
  • Thank you guys, and sorry. Like I said, I'm new to programming and this site. Let me poke at it a bit with the suggestions R Nar made and I'll post the results back. – Brice Petty Nov 24 '15 at 19:22
  • You need to check if number is in D not alist – SirParselot Nov 24 '15 at 19:23
  • You should have worte: `if number in D:` – simpel01 Nov 24 '15 at 19:23
  • Traceback (most recent call last): File "", line 3, in D[number] = D[number]+1 KeyError: 5 – Brice Petty Nov 24 '15 at 19:28
  • Thank you SirParselot & R Nar. I applied your suggestions to my attempt and it is now working. Thank you! – Brice Petty Nov 24 '15 at 19:30
  • updated attempt to solution – Brice Petty Nov 24 '15 at 19:31
  • `Numpy.unique` is the fastest way to count. Take a look. http://stackoverflow.com/questions/10741346/numpy-most-efficient-frequency-counts-for-unique-values-in-an-array `Collection Counter ` is really slow. – Aung Nov 24 '15 at 20:16

5 Answers5

2
>>> from collections import Counter
>>> Counter(alist)
Counter({7: 2, 1: 1, 2: 1, 5: 1, 6: 1, 9: 1})
Ayush
  • 3,695
  • 1
  • 32
  • 42
1

Simplest way to do this is a dictionary comprehension

count_dict = {elem:a_list.count(elem) for elem in a_list}

It's like a for loop but it specifies both the key and value for each dictionary item.

Josh Weinstein
  • 2,788
  • 2
  • 21
  • 38
1

Ayush Shanker's answer is a perfectly valid way to do what you want. I'll point out how to fix the code you have now.

First, in these two lines

for number in alist:
    if number in alist:

You're basically saying "for every number in alist, check to see if it's in alist". Of course it is! So, you really want if number in D: for the second line, because then you're checking to see if the number is in the dictionary you're building.

The second problem is this:

        D[number] = D[number+1]

number and number+1 here are indices, so you're referring to two different elements in D. The solution is to move the +1 outside. This is what the fixed code looks like:

for number in alist:
    if number in D:
        D[number] = D[number] + 1
    else:
        D[number] = 1

To be more Pythonic, you can also replace D[number] = D[number] + 1 with D[number] += 1.

Community
  • 1
  • 1
El'endia Starman
  • 2,204
  • 21
  • 35
0

To fix you code and algorithm, you need to check if n is in D.keys() (which returns list of keys of D dictionary:

>>> D = {}
>>> for n in alist:
    if n in D.keys():
        D[n] += 1
    else:
        D[n] = 1


>>> D
{1: 1, 2: 1, 5: 1, 6: 1, 7: 2, 9: 1}
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
0

SOLUTION:

 def mode(somelist):
        count = {}
        for n in alist:
            if n in count:
                count[n]+=1
            else:
                count[n] = 1
        return(count)
Brice Petty
  • 17
  • 1
  • 4