I'm new in Python.
I created a code that should allow me to find the percentage of items that follows a given item in a list.
Given a list:
list1=["a", "b", "a", "c", "a", "b", "c", "d", "e", "a", "b", "d", "e", "a", "c"]
I would like to find, for each, say, "a", in which percentage every item is following. The code returns:
[(33, 'a'), (25, 'b'), (16, 'e'), (16, 'd'), (16, 'c')]
[(30, 'a'), (20, 'e'), (20, 'd'), (20, 'c'), (20, 'b')]
[(25, 'e'), (25, 'd'), (25, 'b'), (25, 'a'), (12, 'c')]
[(33, 'e'), (33, 'd'), (33, 'b'), (33, 'a')]
[]
The output is right, and it's what i wanted.
But I would also like to sum every key of the different dictionaries, so I can have something like:
[(121, 'a'), (103, 'b'), (94, 'e'), (94, 'd'), (48, 'c')]
I didn't find a way to do that. I know that there are some ways to sum values of every key in different dictionaries, but the problem here is that dictionaries are created inside a for loop, because i need as dictionaries as much target items are (in this case, "a").
I tried to iterate in every dict with
for key, value in dictio.items():
dictio[key]=value + dictio.get(key, 0)
print (dictio)
But the result is a mess, and it's not even far from what I would like to have.
I would like to know from you if it is possible to join multiple dictionaries, without knowing their number (because they are created in a for loop).
And, as I would like to understand better Python logic, I would like not to use external libraries, if it's possibile.
Thank you in advance!
Niccolò