我有一个列表:和值蟒蛇
l = [['a', 10, 30], ['b', 34, 89], ['c', 40, 60],['d',30,20]]
其中每个子列表中的第一个产品的名称和另外两个号码都标记(SUB1和SUB2)
嵌套表可以是动态的,即嵌套列表的数量可以根据功能而改变。
我所寻找的是找到
average of subj 1 i.e (10+34+40+30)/4 and
similarly sub2 (30+89+60+20)/4
also average marks of a: (10+30)/2
average marks of b: (34+89)/2 and so on.
我想:
c = 0
for i in range(0,len(list_marks1)):
c += list_marks1[i][1]
sub_1avg = float(c)/len(list_marks1)
d = 0
for i in range(0,len(list_marks1)):
d += list_marks1[i][2]
sub_2avg = float(d)/len(list_marks1)
,但这是不正确的。
有没有最佳的方法来做到这一点?由于我的嵌套列表中的主题数量也可以改变。
你应该考虑使用'dictionary'代替*可能*更复杂的数据类型 – TheDarkKnight