How to calculate the percentage of each element in a list?
starting from your approach, you could do the rest with a Counter
from collections import Counter
for item in zip(*['123', '134', '234', '214', '223']):
c = Counter(item)
total = sum(c.values())
percent = {key: value/total for key, value in c.items()}
print(percent)
# convert to list
percent_list = [percent.get(str(i), 0.0) for i in range(5)]
print(percent_list)
which prints
{'2': 0.6, '1': 0.4}
[0.0, 0.4, 0.6, 0.0, 0.0]
{'2': 0.4, '3': 0.4, '1': 0.2}
[0.0, 0.2, 0.4, 0.4, 0.0]
{'4': 0.6, '3': 0.4}
[0.0, 0.0, 0.0, 0.4, 0.6]
You could start by creating the zipped list as you did:
zipped = zip(*l)
then map an itertools.Counter
to it as to get the counts of each item in the results from zip
:
counts = map(Counter, zipped)
and then go through it, creating a list out of their counts divided by their sizes:
res = [[c[i]/sum(c.values()) for i in '1234'] for c in counts]
print(res)
[[0.4, 0.6, 0.0, 0.0], [0.2, 0.4, 0.4, 0.0], [0.0, 0.0, 0.4, 0.6]]
If you are a one-liner kind of person, mush the first two in the comprehension to get this in one line:
res = [[c[i]/sum(c.values()) for i in '1234'] for c in map(Counter, zip(*l))]
additionally, as noted in a comment, if you don't know the elements ahead of time, sorted(set(''.join(l)))
could replace '1234'
.