I'm looking to compare a list containing a sequence of numbers and a python dictionary in order to find occurrences of numbers. The behaviour of program:
- The function occurences(L) takes as parameter a list
- The function occurences(L) returns a dictionary whose keys are the elements of the list and the values are the number of times the element appears.
liste = []
def occurrences(liste):
dico = {}
for i in liste:
for k,v in dico.items():
if i == liste[k]:
dico[i] = dico[i] + 1
else:
dico[i] = 1
return dico
Example of result:
>>> occurrences([1,3,2,1,4,1,2,1]) # input
{1:4,2:2,3:1,4:1} # output