我有两个列表,其中第一个列表保证比第二个列表多包含一个条目。我想知道创建一个新列表的最python方法,该列表的偶数索引值来自第一个列表,奇数索引值来自第二个列表。
# example inputs
list1 = ['f', 'o', 'o']
list2 = ['hello', 'world']
# desired output
['f', 'hello', 'o', 'world', 'o']
这很管用,但并不漂亮:
list3 = []
while True:
try:
list3.append(list1.pop(0))
list3.append(list2.pop(0))
except IndexError:
break
不然怎么能做到呢?什么是最Python的方法?
这应该符合您的要求:
在^{} documentation 中有一个配方:
有一种方法可以通过切片来实现:
相关问题 更多 >
编程相关推荐