我想创建一个列表,在python中从2个单独的列表中交替添加元素。
我有以下想法,但似乎不起作用:

t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
t3= [len(t1)+len(t2)]
a = 0

while a < len(t1)+len(t2):
    t3.extend(t1[a])
    t3.extend(t2[a])
    a = a + 1
print t3


所以基本上我想要['Jan',31,'Feb',28,'Mar',31,等等...]

最佳答案

干得好:

t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
      'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
t3 = list()

for i, j in zip(t1, t2):
  t3.append(i)
  t3.append(j)

print(t3)

关于python - python将元素从列表追加到列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37345907/

10-12 22:11