Python – 备用列表元素
给定2个列表,以之字形方式打印元素,即打印列表的相似索引,然后继续下一步。
Input : test_list1 = [5, 3, 1], test_list2 = [6, 4, 2]
Output : [5, 6, 3, 4, 1, 2, 4]
Explanation : 5 and 6, as in 0th index are printed first, then 3 and 4 on 1st index, and so on.
Input : test_list1 = [5, 3, 1, 9], test_list2 = [6, 4, 2, 10]
Output : [5, 6, 3, 4, 1, 2, 4, 9, 10]
Explanation : 5 and 6, as in 0th index are printed first, then 3 and 4 on 1st index, and so on.
方法#1:使用循环
这是可以执行此任务的方式之一。在这种情况下,我们明确地执行迭代并将相似的索引元素一个接一个地附加到结果列表中。
Python3
# Python3 code to demonstrate working of
# Alternate List elements
# Using loop
# initializing lists
test_list1 = [5, 3, 1, 4, 7]
test_list2 = [6, 4, 2, 5, 1]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Using loop to print elements in criss cross manner
res = []
for idx in range(0, len(test_list1)):
res.append(test_list1[idx])
res.append(test_list2[idx])
# printing result
print("The zig-zag printing of elements : " + str(res))
Python3
# Python3 code to demonstrate working of
# Alternate List elements
# Using zip() + loop
# initializing lists
test_list1 = [5, 3, 1, 4, 7]
test_list2 = [6, 4, 2, 5, 1]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Using zip() to perform pairing and loop to
# get elements into result list
res = []
for ele1, ele2 in zip(test_list1, test_list2):
res.append(ele1)
res.append(ele2)
# printing result
print("The zig-zag printing of elements : " + str(res))
输出
The original list 1 : [5, 3, 1, 4, 7]
The original list 2 : [6, 4, 2, 5, 1]
The zig-zag printing of elements : [5, 6, 3, 4, 1, 2, 4, 5, 7, 1]
方法 #2:使用 zip() + 循环
上述功能的组合可以用来解决这个问题。在此,我们使用 zip() 将每个元素与相似的索引配对,然后使用循环将每个元素输入结果列表。
Python3
# Python3 code to demonstrate working of
# Alternate List elements
# Using zip() + loop
# initializing lists
test_list1 = [5, 3, 1, 4, 7]
test_list2 = [6, 4, 2, 5, 1]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Using zip() to perform pairing and loop to
# get elements into result list
res = []
for ele1, ele2 in zip(test_list1, test_list2):
res.append(ele1)
res.append(ele2)
# printing result
print("The zig-zag printing of elements : " + str(res))
输出
The original list 1 : [5, 3, 1, 4, 7]
The original list 2 : [6, 4, 2, 5, 1]
The zig-zag printing of elements : [5, 6, 3, 4, 1, 2, 4, 5, 7, 1]