刚开始学python,被这个问题卡住了:我有这个清单:data = [ ['Grinning', 2.26, 1.02, 87.3], ['Beaming', 19.1, 1.69, 150.0], ['ROFL', 25.6, 0.774, 0.0], ['Tears of Joy', 233.0, 7.31, 2270.0], ['Winking', 15.2, 2.36, 264.0], ['Happy', 22.7, 4.26, 565.0], ['Heart Eyes', 64.6, 11.2, 834.0], ['Kissing', 87.5, 5.13, 432.0], ['Thinking', 6.81, 0.636, 0.0], ['Unamused', 6.0, 0.236, 478.0], ['Sunglasses', 4.72, 3.93, 198.0], ['Loudly Crying', 24.7, 1.35, 654.0], ['Kiss Mark', 21.7, 2.87, 98.7], ['Two Hearts', 10.0, 5.69, 445.0], ['Heart', 118.0, 26.0, 1080.0], ['Heart Suit', 3.31, 1.82, 697.0], ['Thumbs Up', 23.1, 3.75, 227.0], ['Shrugging', 1.74, 0.11, 0.0], ['Fire', 4.5, 2.49, 150.0], ['Recycle', 0.0333, 0.056, 932.0]]我需要总结每一行数字,例如:"Grinning" sum = 2.26 + 1.02 + 87.3我尝试了很多方法来只获取行号但是没有找到一种可以正常工作并且不会总结所有列的方法我也试过这个:totalemoji = 0for i in range(len(data)): for i in range(1, 3): totalemoji += data[1][i] + data[1][i] + data[1][i]print(totalemoji)但它打印出一个我什至不认识的数字......如果有人可以解释如何对每个行号求和,我将不胜感激
3 回答
手掌心
TA贡献1942条经验 获得超3个赞
选项 1:没有sum按要求
d = {}
for current_sub_list in data:
current_list_sum = 0
for j in range(1, len(current_sub_list)):
current_list_sum += current_sub_list[j]
d[current_sub_list[0]] = current_list_sum
print(d)
选项 2:
d = {}
for x in data:
d[x[0]] = sum(x[1:])
print(d)
烙印99
TA贡献1829条经验 获得超13个赞
x=[]
for i in range(len(data)):
x.append([data[i][0], sum(data[i][1:])])
print(x)
这里 x 是list一个list
添加回答
举报
0/150
提交
取消