因此,列表清单是:
[['1', '1', 'tall', 8.425169446611104], ['1', '2', 'tall', 8.917085904771866], ['1', '3', 'medium', 6.174348482965436], ['2', '1', 'short', 7.8163095871050965], ['2', '2', 'medium', 9.098197168204184], ['2', '3', 'medium', 7.889044875446846], ['3', '1', 'medium', 7.267435895701576], ['3', '2', 'medium', 2.270144244358967], ['3', '3', 'tall', 9.721626339195156], ['4', '1', 'medium', 8.242226639616785], ['4', '2', 'tall', 11.688990983183421], ['4', '3', 'medium', 5.326719989412714]]
所以我会试着解释一下我想在这里做些什么:
如果我们拿出两个列表:
['1','1','tall',8.425169446611104]
['2','1','short',7.8163095871050965]
如果列表中的第二个值与列表中的另一个列表匹配(在本例中为“1”),您将获取最后一个数字(8.425169446611104)和另一个数字(7.8163095871050965)并将其除以通过数字(2)来获得平均数。
['1','X','tall',Y]
['2','X','短',Y]
TLDR:如果X'匹配,则将两个Y加在一起并除以Y的数量(平均Y)(因此它可以是两个具有相同X数的列表)
所以我想要的是具有相同X'的 Y的平均值。
答案 0 :(得分:2)
这是一种蛮力,我可以更有效地完成它,但是对于你来说启动是一个好地方。
lists = [['1', '1', 'tall', 8.425169446611104], ['1', '2', 'tall', 8.917085904771866], ['1', '3', 'medium', 6.174348482965436], ['2', '1', 'short', 7.8163095871050965], ['2', '2', 'medium', 9.098197168204184], ['2', '3', 'medium', 7.889044875446846], ['3', '1', 'medium', 7.267435895701576], ['3', '2', 'medium', 2.270144244358967], ['3', '3', 'tall', 9.721626339195156], ['4', '1', 'medium', 8.242226639616785], ['4', '2', 'tall', 11.688990983183421], ['4', '3', 'medium', 5.326719989412714]]
# Creating a dictionary of each "second element" in the sublists and
# compute the 'subtotal' for each of those values:
d = dict()
for i in range(len(lists)):
d[lists[i][1]] = lists[i][3] + d.get(lists[i][1], 0)
# now do the division:
for k in d.keys():
count = len([l for l in lists if l[1] == k])
print 'average for {} is {}'.format(k, d[k]/count)
这应该打印出以下内容:
{'1': 31.751141569034562, '3': 29.111739687020155, '2': 31.974418300518437}
average for 1 is 7.93778539226
average for 3 is 7.27793492176
average for 2 is 7.99360457513
答案 1 :(得分:0)
from collections import defaultdict
all_lists = [['1', '1', 'tall', 8.425169446611104],
['1', '2', 'tall', 8.917085904771866],
['1', '3', 'medium', 6.174348482965436],
['2', '1', 'short', 7.8163095871050965],
['2', '2', 'medium', 9.098197168204184],
['2', '3', 'medium', 7.889044875446846],
['3', '1', 'medium', 7.267435895701576],
['3', '2', 'medium', 2.270144244358967],
['3', '3', 'tall', 9.721626339195156],
['4', '1', 'medium', 8.242226639616785],
['4', '2', 'tall', 11.688990983183421],
['4', '3', 'medium', 5.326719989412714]]
sublists = defaultdict(list)
for sublist in all_lists:
sublists[sublist[1]].append(sublist[3])
result = {key: sum(sublist) / len(sublist)
for key, sublist in sublists.items()}
print result
# {'1': 7.9377853922586405, '3': 7.277934921755039, '2': 7.993604575129609}
答案 2 :(得分:-1)
试着告诉我这是不是你想要的
list_v = [YOUR LIST HERE]
previous_numbers = []
for x in list_v:
if x[1] not in previous_numbers:
division = 1
value = x[3]
for k in list_v:
if x!=k:
if x[1] == k[1]:
value += k[3]
division += 1
value = value/division
print x[1]," has medium value: ",value
previous_numbers.append(x[1])
输出:
1 has medium value: 7.93778539226
2 has medium value: 7.99360457513
3 has medium value: 7.27793492176
不是非常pythonic但它具有新手可读的优势