列表中的python平均列数

问题描述 投票:1回答:7

我有四张相同的len列表

list1=[26, 26, 26, 27, 27, 27, 27, 28, 28, ..., 100, 100, 100]
list2=[-1, -2, 10, 14, 13, 15, 20, -4, -10,...., 90, 10,  -1]
list3=[11, 12, -3, -4, 10, 11, 12, 13, 14, ..., -1, -1, -1]
list4=[50, 60, 70, 90, 30, 40, 20, 20, 10, ...., 20, 20, 20]

我必须平均list2, 3, and 4中的值,这些值在list1中具有相同的值。我的意思是,例如,对于list2,我想要(-1+-2+10)/3=2.33,因为list1中的-1, -2, and 10的相应元素是26(14+13+15+20)/4=15.5 (four corresponding 27s in list1).基本上,同样的想法也适用于list3 and list4。对于list3,我想要(11+12+-3)/3=6.67

最终,在转换和平均后,4个列表是:

list1=[26, 27, 28, ...., 100]
list2=[2.33, 15.5, -7, ..., 33]
list3=[6.667, 7.25, 13.5,.., -1]
list4=[60, 45, 15, ..., 20]

我在考虑这样的事情。绝对有一种更优雅的方式来做到这一点。

for x, y, z, q in zip(list1, list2, list3, list4):
    if x==previous x:
       #same x, add y, z, q, to separate temp lists (when new x appears, average out)
    else:
       #new x, average out y, z, q (empty temp lists)   
python list mean
7个回答
2
投票

假设第一个列表已经排序,你可以这样做,以平均其他列表中与list1中相同数字相对应的数字:

list1=[26, 26, 26, 27, 27, 27, 27, 28, 28, 100, 100, 100]
list2=[-1, -2, 10, 14, 13, 15, 20, -4, -10, 90, 10,  -1]
list3=[11, 12, -3, -4, 10, 11, 12, 13, 14, -1, -1, -1]
list4=[50, 60, 70, 90, 30, 40, 20, 20, 10, 20, 20, 20]

# Transformed Lists
list1_new = []
list2_new = []
list3_new = []
list4_new = []

def populate_new_list():
    list1_new.append(list1[start_index])
    # If using python 3, we don't need to convert int to float before division
    list2_new.append( float(sum(list2[start_index:end_index+1])) / float((end_index-start_index+1)) )
    list3_new.append( float(sum(list3[start_index:end_index+1])) / float((end_index-start_index+1)) )
    list4_new.append( float(sum(list4[start_index:end_index+1])) / float((end_index-start_index+1)) )

start_index = 0 # Start index of same number in first list
end_index = 0 # End index of same number in first list
previous_item = list1[0] # Initialize previous_item with first item in the list

# Iterate through the first list and store start and end index of duplicate numbers, then use populate_new_list to average those numbers.
for index, item in enumerate(list1):
    if previous_item == item:
        end_index = index
    else:
        populate_new_list()
        start_index = index
        end_index = index
        previous_item = list1[index]

# Call populate_new_list once more after iterating through list1 to populate lists with last same number in list1
populate_new_list()

print(list1_new) # [26, 27, 28, 100]
print(list2_new) # [2.3333333333333335, 15.5, -7.0, 33.0]
print(list3_new) # [6.666666666666667, 7.25, 13.5, -1.0]
print(list4_new) # [60.0, 45.0, 15.0, 20.0]

1
投票

使用collections.OrderedDict会更好:

list1=[26, 26, 26, 27, 27, 27, 27, 28, 28, 100, 100, 100]
list2=[-1, -2, 10, 14, 13, 15, 20, -4, -10, 90, 10,  -1]
list3=[11, 12, -3, -4, 10, 11, 12, 13, 14, -1, -1, -1]
list4=[50, 60, 70, 90, 30, 40, 20, 20, 10, 20, 20, 20]

from collections import OrderedDict
def refine_list(lst):
    d = OrderedDict()
    for index, value in enumerate(lst):
        d.setdefault(list1[index], []).append(value)
    result = []
    for value in d.values():
        result.append(sum(value) / len(value))
    return result

print(list(set(list1)))
for lst in [list2, list3, list4]:
    print(refine_list(lst))


[100, 26, 27, 28]
[2.3333333333333335, 15.5, -7.0, 33.0]
[6.666666666666667, 7.25, 13.5, -1.0]
[60.0, 45.0, 15.0, 20.0]

1
投票

这将有效 -

list1=[26, 26, 26, 27, 27, 27, 27, 28, 28]
list2=[-1, -2, 10, 14, 13, 15, 20, -4, -10]
list3=[11, 12, -3, -4, 10, 11, 12, 13, 1]
list4=[50, 60, 70, 90, 30, 40, 20, 20, 10]

a={}
b={}
c={}
for i in range(len(list1)):
    if a.get(list1[i]) is None:
        a[list1[i]] = [list2[i]]
        b[list1[i]] = [list3[i]]
        c[list1[i]] = [list4[i]]
    else:
        a[list1[i]] += [list2[i]]
        b[list1[i]] += [list3[i]]
        c[list1[i]] += [list4[i]]
a = { i: (sum(l) / len(l)) for i,l in a.items()}
b = { i: (sum(l) / len(l)) for i,l in b.items()}
c = { i: (sum(l) / len(l)) for i,l in c.items()}
list1 = list(a.keys())
list2 = list(a.values())
list3 = list(b.values())
list4 = list(c.values())
print(list1, list2, list3, list4)

产量

[26, 27, 28] [2.3333333333333335, 15.5, -7.0] [6.666666666666667, 7.25, 7.0] [60.0, 45.0, 15.0]

说明

让我们回顾一下这些部分(我会为其中一个案例进行宣传,休息是很好的旧复制粘贴:)) -

宣布3 dict -

a={}
b={}
c={}

这里,假设所有列表具有相同的长度。对于列表中的每个元素,您将创建一个dict值。例如,对于26中的元素list1,此操作后的输出将为{26: [-1, -2, 10], 27: [14, 13, 15, 20], 28: [-4, -10]}

for i in range(len(list1)):
    if a.get(list1[i]) is None:
        a[list1[i]] = [list2[i]]
    else:
        a[list1[i]] += [list2[i]]

一旦完成,你只需在key中为每个dict列出平均值 -

a = { i: (sum(l) / len(l)) for i,l in a.items()}

1
投票

您可以在list1中获取一组唯一的值并迭代它们。然后,您可以在list2-4中找到相同索引处的值。获取各自的平均值并附加到新的临时列表。

from copy import deepcopy

list1=[26, 26, 26, 27, 27, 27, 27, 28, 28]
list2=[-1, -2, 10, 14, 13, 15, 20, -4, -10]
list3=[11, 12, -3, -4, 10, 11, 12, 13, 14]
list4=[50, 60, 70, 90, 30, 40, 20, 20, 10]

temp_list2 = []
temp_list3 = []
temp_list4 = []

for val in set(list1):
    # List 2
    total = [list2[ix] for ix, i in enumerate(list1) if i == val]
    mean = sum(total) / len(total)
    temp_list2.append(mean)

    # List 3
    total = [list3[ix] for ix, i in enumerate(list1) if i == val]
    mean = sum(total) / len(total)
    temp_list3.append(mean)

    # List 4
    total = [list4[ix] for ix, i in enumerate(list1) if i == val]
    mean = sum(total) / len(total)
    temp_list4.append(mean)

list1 = list(set(list1))
list2 = deepcopy(temp_list2)
list3 = deepcopy(temp_list3)
list4 = deepcopy(temp_list4)

print(list1)
print(list2)
print(list3)
print(list4)

[26, 27, 28] [2.3333333333333335, 15.5, -7.0] [6.666666666666667, 7.25, 13.5] [60.0, 45.0, 15.0]


1
投票

在我的回答中有一个假设,即所有列表都具有相同的长度。但即使list1被洗牌,这段代码也能正常工作。

l1 = [26, 26, 26, 27, 27, 28, 28, 28, 29, 29, 29, 29]
l2 = [-1, -2, 10, 14, 13, 15, 20, -4, -10, 90, 10,  -1]
l3=[11, 12, -3, -4, 10, 11, 12, 13, 14, -1, -1, -1]
l4=[50, 60, 70, 90, 30, 40, 20, 20, 10, 20, 20, 20]

temp_list_2 = []
temp_list_3 = []
temp_list_4 = []
count = 0

#set() finds all unique elements
for i in set(l1):
    i_count = l1.count(i) # counts number of elements in list 1
    temp2 = l2[count:count+i_count]
    temp3 = l3[count:count+i_count]
    temp4 = l4[count:count+i_count]

    count = count + i_count
    avg2 = sum(temp2)/len(temp2)
    avg3 = sum(temp3)/len(temp3)
    avg4 = sum(temp4)/len(temp4)

    temp_list_2.append(avg2)
    temp_list_3.append(avg3)
    temp_list_4.append(avg4)

print(temp_list_2)
print(temp_list_3)
print(temp_list_4)

>> [2.3333333333333335, 13.5, 10.333333333333334, 22.25]
[6.666666666666667, 3.0, 12.0, 2.75]
[60.0, 60.0, 26.666666666666668, 17.5]

1
投票

你可以使用itertool的groupby

from itertools import groupby
from statistics import mean

list1=[26, 26, 26, 27, 27, 27, 27, 28, 28]
list2=[-1, -2, 10, 14, 13, 15, 20, -4, -10]
list3=[11, 12, -3, -4, 10, 11, 12, 13, 14]
list4=[50, 60, 70, 90, 30, 40, 20, 20, 10]

def avg_from_list(list1, list_n):
    new_list = []
    for key, group in groupby(enumerate(list1), key=lambda x: x[1]):
        new_list.append(mean([list_n[i] for i, _ in group]))
    return new_list

print(sorted(list(set(list1))))
for l in (list2, list3, list4):
    print(avg_from_list(list1, l))

输出:

[26, 27, 28]
[2.3333333333333335, 15.5, -7]
[6.666666666666667, 7.25, 13.5]
[60, 45, 15]

0
投票

使用Python的groupby()以相同的值读取块中的list1。为每个元素分配一个索引,然后由itergetter()使用该索引,以便从所有四个列表中提取相同的索引:

from itertools import groupby
from operator import itemgetter

list1 = [26, 26, 26, 27, 27, 27, 27, 28, 28, 100, 100, 100]
list2 = [-1, -2, 10, 14, 13, 15, 20, -4, -10, 90, 10,  -1]
list3 = [11, 12, -3, -4, 10, 11, 12, 13, 14, -1, -1, -1]
list4 = [50, 60, 70, 90, 30, 40, 20, 20, 10, 20, 20, 20]

output = [[], [], [], []]

for k, g in groupby(zip(list1, range(len(list1))), key=lambda x: x[0]):
    req_cols = itemgetter(*[i for v, i in g])

    for index, l in enumerate([list1, list2, list3, list4]):
        cols = req_cols(l)
        output[index].append(sum(cols) / float(len(cols)))

print output

# Reassign the updated lists back if needed
list1 = output[0]
list2 = output[1]
list3 = output[2]
list4 = output[3]

给你:

[[26.0, 27.0, 28.0, 100.0], [2.3333333333333335, 15.5, -7.0, 33.0], [6.666666666666667, 7.25, 13.5, -1.0], [60.0, 45.0, 15.0, 20.0]]
© www.soinside.com 2019 - 2024. All rights reserved.