【发布时间】:2017-06-01 08:52:15
【问题描述】:
我有一个清单:
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代替可能更复杂的数据类型