[英]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.