You can first perform a group by. We can do this for instance with a defaultdict
:
from collections import defaultdict
col = defaultdict(list)
for ai,bi in zip(a,b):
col[ai].append(bi)
Now the dictionary col
will look like:
>>> col
defaultdict(<class 'list'>, {'C': [3, 2], 'B': [4], 'D': [5], 'A': [2, 8, 1]})
and now we can calculate the average of all elements in the dictionary for instance like:
>>> {key:sum(vals)/len(vals) for key,vals in col.items()}
{'C': 2.5, 'B': 4.0, 'D': 5.0, 'A': 3.6666666666666665}
You can also convert it to two tuples by using zip
:
a,b = zip(*[(key,sum(vals)/len(vals)) for key,vals in col.items()])
resulting in:
>>> a,b = zip(*[(key,sum(vals)/len(vals)) for key,vals in col.items()])
>>> a
('C', 'B', 'D', 'A')
>>> b
(2.5, 4.0, 5.0, 3.6666666666666665)
If you want to generate lists instead, you can convert them to lists:
a,b = map(list,zip(*[(key,sum(vals)/len(vals)) for key,vals in col.items()]))
This results in:
>>> a,b = map(list,zip(*[(key,sum(vals)/len(vals)) for key,vals in col.items()]))
>>> a
['C', 'B', 'D', 'A']
>>> b
[2.5, 4.0, 5.0, 3.6666666666666665]