我似乎无法弄清楚如何做到这一点......我有一个我想要的结果的例子,但我无法弄清楚如何用循环做到这一点。
celts = [["Bass", 1,2,3],
["Bradley", 7,8,9]]
celts2 = [["Bass", 4,5,6],
["Bradley", 1,2,3]]
celts3 = [["Bass", 8, 5, 2],
["Bradely", 7,4,1]]
new = celts[0] + celts2[0] + celts3[0], celts[1] + celts2[1] + celts3[1],
print new
结果是:
(['Bass', 1, 2, 3, 'Bass', 4, 5, 6, 'Bass', 8, 5, 2], ['Bradley', 7, 8, 9, 'Bradley', 1, 2, 3, 'Bradely', 7, 4, 1])
答案 0 :(得分:2)
使用列表推导(是一种循环):
celts = [["Bass", 1,2,3],
["Bradley", 7,8,9]]
celts2 = [["Bass", 4,5,6],
["Bradley", 1,2,3]]
celts3 = [["Bass", 8, 5, 2],
["Bradely", 7,4,1]]
new = [celts[i] + celts2[i] + celts3[i] for i in range(len(celts))]
>>> print new
[['Bass', 1, 2, 3, 'Bass', 4, 5, 6, 'Bass', 8, 5, 2], ['Bradley', 7, 8, 9, 'Bradley', 1, 2, 3, 'Bradely', 7, 4, 1]]
答案 1 :(得分:1)
这应该符合您的意图:
allcelts = [[["Bass", 1,2,3],
["Bradley", 7,8,9]],
[["Bass", 4,5,6],
["Bradley", 1,2,3]],
[["Bass", 8, 5, 2],
["Bradely", 7,4,1]]]
new = []
for i in range(len(allcelts[0])):
element = []
for celts in allcelts:
element.extend(celts[i])
new.append(element)
答案 2 :(得分:0)
使用,单独的元素会产生一个元组,如果你想要一个列表,你可以用[]
包含 new = [celts[0] + celts2[0] + celts3[0], celts[1] + celts2[1] + celts3[1],]