我如何将两个列表放在一起,但在 Python 中 TURN-BY-TURN
How do I put two list together as one but TURN-BY-TURN in Python
我有这个列表:
a = ["1 Monday","1 Wednesday","1 Friday"]
b = ["2 Tuesday","2 Thursday","2 Saturday"]
我想将这些结合起来:
c = ["1 Monday", "2 Tuesday", "1 Wednesday", "2 Thursday", "1 Friday", "2 Saturday"]
我想轮流做这个。因此,附加 a 的第一个元素和 b 的第一个元素,然后附加 a 的第二个元素和 b 的第二个元素等。
您可以将 itertools
与 zip
一起使用:
In [3585]: import itertools
In [3586]: list(itertools.chain(*zip(a,b)))
Out[3586]:
['1 Monday',
'2 Tuesday',
'1 Wednesday',
'2 Thursday',
'1 Friday',
'2 Saturday']
基本解
list_turn = []
a = ["1 Monday","1 Wednesday","1 Friday"]
b = ["2 Tuesday","2 Thursday","2 Saturday"]
for i in range(len(a)):
list_turn.append(a[i])
list_turn.append(b[i])
我有这个列表:
a = ["1 Monday","1 Wednesday","1 Friday"]
b = ["2 Tuesday","2 Thursday","2 Saturday"]
我想将这些结合起来:
c = ["1 Monday", "2 Tuesday", "1 Wednesday", "2 Thursday", "1 Friday", "2 Saturday"]
我想轮流做这个。因此,附加 a 的第一个元素和 b 的第一个元素,然后附加 a 的第二个元素和 b 的第二个元素等。
您可以将 itertools
与 zip
一起使用:
In [3585]: import itertools
In [3586]: list(itertools.chain(*zip(a,b)))
Out[3586]:
['1 Monday',
'2 Tuesday',
'1 Wednesday',
'2 Thursday',
'1 Friday',
'2 Saturday']
基本解
list_turn = []
a = ["1 Monday","1 Wednesday","1 Friday"]
b = ["2 Tuesday","2 Thursday","2 Saturday"]
for i in range(len(a)):
list_turn.append(a[i])
list_turn.append(b[i])