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