[英]Python zip() without parantheses
我正在尝试使用 zip() function 根据用户输入组合两个列表。 但我希望结果没有额外的括号。
lst1 = []
# For list of strings/chars
lst2 = []
lst1 = [int(item) for item in input("Enter the list items : ").split()]
lst2 = [int(item) for item in input("Enter the list items : ").split()]
print(lst1)
print(lst2)
print([list(a) for a in zip(lst1,lst2)])
Output = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]
Desired output = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
看到这个答案。
print([e for t in zip(lst1, lst2) for e in t])
from itertools import chain
lst1 = []
# For list of strings/chars
lst2 = []
lst1 = [int(item) for item in input("Enter the list items : ").split()]
lst2 = [int(item) for item in input("Enter the list items : ").split()]
# fist method
print(list(chain.from_iterable([a for a in zip(lst1,lst2)])))
# second method
total_list = []
for a, b in zip(lst1,lst2):
total_list.append(a)
total_list.append(b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.