简体   繁体   中英

How can I remove duplicates in a list and change their corresponding values in another list (by index position) to the mean?

Suppose I have two lists:

list1 = [1200,1200,1200,2000]
list2 = [12000,14000,13000,30000]

How can I remove the duplicates in list1 and take the mean of all corresponding values in list2? eg 1200 matches with 12000, 1200 matches with 14000 and 1200 matches with 13000 --> remove duplicate 1200s and take the mean of (12000,14000 and 13000) and return that in their place in list2 (13000). This should produce:

list1new = [1200,2000]
list2new = [13000,30000]

Thanks

Try:

from statistics import mean

list1 = [1200, 1200, 1200, 2000]
list2 = [12000, 14000, 13000, 30000]

tmp = {}
for a, b in zip(list1, list2):
    tmp.setdefault(a, []).append(b)

list1new, list2new = [], []
for k, v in tmp.items():
    list1new.append(k)
    list2new.append(mean(v))

print(list1new)
print(list2new)

Prints:

[1200, 2000]
[13000, 30000]
import pandas as pd
df = pd.DataFrame({'l1':list1, 'l2': list2})
tmp = df.groupby('l1')['l2'].mean()
new_list1 = list(tmp.index)
new_list2 = list(tmp)
print(new_list1)
print(new_list2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM