Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a dictionary from two lists? While taking an average of values?

Wizards of stackoverflow,

I wish to combine two lists to create a dictionary, I have used dict & zip, however it does not meet what I require.

If had these lists

  keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]

I would like for the dictionary to reflect the average value such that the output would be:

a_dict = {'a' : 4, 'b' : 3, 'c' : 4}

as a bonus but not required, if this is possible is there anyway to get a count of each duplicate? i.e. output would be followed by 'a' was counted twice, other than just doing the count in the keys.

like image 745
Samuel Millington Avatar asked Feb 03 '26 22:02

Samuel Millington


2 Answers

A straightforward solution (thanks @DeepSpace for dict-comprehension suggestion):

keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]

out = {}
for k, v in zip(keys, values):
    out.setdefault(k, []).append(v)

out = {key: sum(value) / len(value) for key, value in out.items()}

print(out)

Prints:

{'a': 4.0, 'b': 3.0, 'c': 4.0}

If you want count of keys, you can do for example:

out = {}
for k, v in zip(keys, values):
    out.setdefault(k, []).append(v)

out = {key: (sum(value) / len(value), len(value)) for key, value in out.items()}

print(out)

Prints:

{'a': (4.0, 2), 'b': (3.0, 1), 'c': (4.0, 1)}

Where the second element of values is a count of key.


Solution with itertools (if keys are sorted):

keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]

from itertools import groupby
from statistics import mean

out = {}
for k, g in groupby(zip(keys, values), lambda k: k[0]):
    out[k] = mean(v for _, v in g)

print(out)

Prints:

{'a': 4, 'b': 3, 'c': 4}
like image 94
Andrej Kesely Avatar answered Feb 05 '26 11:02

Andrej Kesely


calculating avg and frequency of each key dic = {key: [avg, frequency]}

keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]

dic = {i:[[], 0] for i in keys}


for k, v in zip(keys, values):
    dic[k][0].append(v)
    dic[k][1]+=1

for k, v in dic.items():
    dic[k][0] = sum(dic[k][0])/len(dic[k][0])

print(dic)

output

{'a': [4.0, 2], 'b': [3.0, 1], 'c': [4.0, 1]}
like image 27
sahasrara62 Avatar answered Feb 05 '26 12:02

sahasrara62



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!