如何将来自不同列表的数据合并到各自的行中?
How to combine data from different list in their respective row?
比如我有:
list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]
combinedlist=[]
如何确保这些值在各自的行中相加,以便 combinedlist
的输出为:
[9, 10, 6, 14]
list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]
out = [sum(v) for v in zip(list1, list2, list3)]
print(out)
打印:
[9, 10, 6, 14]
你可以试试:
list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]
combinedlist = [list1[i] + list2[i] + list3[i] for i in range(len(list1))]
print(combinedlist)
输出:
[9, 10, 6, 14]
比如我有:
list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]
combinedlist=[]
如何确保这些值在各自的行中相加,以便 combinedlist
的输出为:
[9, 10, 6, 14]
list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]
out = [sum(v) for v in zip(list1, list2, list3)]
print(out)
打印:
[9, 10, 6, 14]
你可以试试:
list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]
combinedlist = [list1[i] + list2[i] + list3[i] for i in range(len(list1))]
print(combinedlist)
输出:
[9, 10, 6, 14]