1
 x = [20, 6, 99, 3, 6, 2, 1,11,41, 31, 99, 6, 7, 8, 99, 10, 99 ,6] 

    y =[]
    for j in x:
        y[j]+=1

I want to count the occurrence of the list values in x. I get an error when I try to do that using the above code. what is wrong In my code?

I am new to programming so kindly explain it in simple terms. my desired output is a list that contains the number of occurences of the values in x.

5 Answers5

3

You should use a dictionary instead of a list, and also be careful - you can't add +1 if the value doesn't exist in the dictionary, which is why you should use .get(j, 0) (zero will be returned as a default value in case it's the first occurrence):

x = [20, 6, 99, 3, 6, 2, 1,11,41, 31, 99, 6, 7, 8, 99, 10, 99 ,6] 

y ={}
for j in x:
    y[j] = y.get(j,0) + 1
print y # {1: 1, 2: 1, 3: 1, 6: 4, 7: 1, 8: 1, 41: 1, 10: 1, 11: 1, 99: 4, 20: 1, 31: 1}
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

In your code y is a list. So y[j] for j=6, for example gets you the 6th element of y. Instead, what you want to do is initialize y as a dict, like y={}.

Your code would become:

y = {}
for j in x:
    y[j] = y.get(j,0) + 1

An even better way for Python 2.6+ would be to use a Counter

Tejas Pendse
  • 551
  • 6
  • 19
0

You could use collection.counter function.

>>> from collections import Counter
>>> x = [20, 6, 99, 3, 6, 2, 1,11,41, 31, 99, 6, 7, 8, 99, 10, 99 ,6]
>>> Counter(x)
Counter({99: 4, 6: 4, 1: 1, 2: 1, 7: 1, 8: 1, 41: 1, 10: 1, 11: 1, 3: 1, 20: 1, 31: 1})
>>> dict(Counter(x))
{1: 1, 2: 1, 99: 4, 6: 4, 7: 1, 8: 1, 41: 1, 10: 1, 11: 1, 3: 1, 20: 1, 31: 1}
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

You can't just access elements of an array if the computer doesn't know the size of the array. Imagine the computer thought your array y had length 10, and set aside a small block of memory for it, and used the rest of its memory for other stuff, like computer games. Now imagine one of the numbers in x was 10000000000. Then the computer would have to modify a cell far, far away from the start of the array, which would be bad, because the computer was already using that memory for other stuff, and you didn't warn it in advance.

In lower level languages, like C, you do this more explicitly by first declaring an array (which sets aside memory for it), then assigning values to its elements.

In your example, your array y actually has length 0, so the computer understandably balks when you try to access elements of it.

Jessica
  • 2,335
  • 2
  • 23
  • 36
0

There is also Counter in collections.

from collections import Counter

x = [20, 6, 99, 3, 6, 2, 1, 11, 41, 31, 99, 6, 7, 8, 99, 10, 99, 6] 
cnt = Counter(x)

Example views of the output:

dict(cnt)
Out[113]: {1: 1, 2: 1, 3: 1, 6: 4, 7: 1, 8: 1, 10: 1, 11: 1, 20: 1, 31: 1, 41: 1, 99: 4}

list(cnt)
Out[114]: [1, 2, 3, 6, 7, 8, 41, 10, 11, 99, 20, 31]

list(cnt.viewitems())
Out[115]: 
[(1, 1),
 (2, 1),
 (3, 1),
 (6, 4),
 (7, 1),
 (8, 1),
 (41, 1),
 (10, 1),
 (11, 1),
 (99, 4),
 (20, 1),
 (31, 1)]
Alexander
  • 105,104
  • 32
  • 201
  • 196