我需要将未知数量的列表中的每个索引一起添加到一个列表中。通过索引添加未知数量的列表
我的意思的例子:
list_a = [1,2,3,4,5]
list_b = [2,4,6,8,10]
list_c = [3,6,9,12,15]
def sum_lists():
temp_list = []
for index in range(len(list_a)):
temp_list.append(list_a[index] + list_b[index] + list_c[index])
return temp_list
total = sum_lists()
我的示例代码的预期结果将是:
total = [6,12,18,24,30]
我将如何完成列表的未知量的总和,例如20个列表?我不需要在数以千计的列表中添加这些内容,但我不知道最初必须添加多少个列表。
任何帮助将不胜感激。所有的列表将具有相同的长度。
如果你要得到一个列表的总和(如:'[1,2,3]→6') ,你会怎么做? – Ryan
'邮编'的名单。在得到的元组列表中,对每个元组的元素进行求和。查看如何使用'zip'和'sum'方法。 – Prune
'[zip(list_a,list_b,list_c)]中的els的总和(els)]。正如@Prune所说,它使用['sum'](https://docs.python.org/3/library/functions.html#sum)和['zip'](https://docs.python.org /3/library/functions.html#zip)内置方法。 –