I have a list:
selection_list=[3, 2, 3, 2, 2, 2]
I want to count cumulatively the occurrences of each element, I want the output to be:
['1/2', '1/4' , '2/2', '2/4', '3/4', '4/4'] # 1/2 means: first occurrence of two
dico={'selection':[], 'Number':[]}
l=[]
keys=[]
for i in range(len(selection_list)):
dico['selection'].append(selection_list[i])
#counting the total occurrences of each element
occurrences = collections.Counter(selection_list)
for i in occurrences.keys():
keys.append(i)
#indexes of each element of the list
for j in range(len(keys)):
l.append([i for i,val in enumerate(selection_list) if val==keys[j]])
for i in range(len(l)):
for j in l[i] :
dico['Number'].insert(int(j), str(len(l[i]))+'-'+ str(len(l[i])) )
I'm getting this output:
dico={'selection': [2, 3, 2, 3, 3, 3], 'UnfoldingNumber': ['2-2', '4-4', '2-2', '4-4', '4-4', '4-4']}
what am I missing?
This is one example for a straightforward solution:
from collections import Counter
selection_list=[3, 2, 3, 2, 2, 2]
numerator = {i:0 for i in set(selection_list)}
denominator = Counter(selection_list)
result = []
for v in selection_list:
numerator[v] += 1
result.append(str(numerator[v]) + '/' + str(denominator[v]))
print(result)