Home > Enterprise >  Can we get duplicated values and get the mean of the value from lists
Can we get duplicated values and get the mean of the value from lists

Time:06-27

Hello this is using python panda

from collections import defaultdict
Unknown_dict = defaultdict(list)
for j, k in zip(my_unknown_id, my_unknown_intensity):
    Unknown_dict[j].append(k)

print(Unknown_dict)

From the above code what I get is

defaultdict(<class 'list'>, {'ABC': [123, 345, 678], 'JIK': [456, 789], 'KIL': [100], 'JAL': [200], 'HON': [300]})

From this list I want to actually get the mean of the values inside the class list. For example for ABC I want to get (123 345 678)/3 and for JIK (456 789)/2.

I tried getting the mean of the values but I found out inside lists we cannot change values. I'm not sure if I got it wrong or does anyone know how to get the mean inside a list?

CodePudding user response:

Once you have the Unknown_list you can iterate and use dict comprehension -

{k: sum(v)/len(v) for k, v in Unknown_list.items()} 

CodePudding user response:

You can get the mean with a comprehension -

d = defaultdict(list, {'ABC': [123, 345, 678], 'JIK': [456, 789], 'KIL': [100], 'JAL': [200], 'HON': [300]})
mean_d = {k: sum(v) / len(v) for k, v in d.items()}
# {'ABC': 382.0, 'JIK': 622.5, 'KIL': 100.0, 'JAL': 200.0, 'HON': 300.0}

If you want to add these mean values back into the original dict -

for k in mean_d:
    d[k].append(mean_d[k])

Output

defaultdict(list,
            {'ABC': [123, 345, 678, 382.0],
             'JIK': [456, 789, 622.5],
             'KIL': [100, 100.0],
             'JAL': [200, 200.0],
             'HON': [300, 300.0]})
  • Related