簡體   English   中英

將兩個列表合並為一個

[英]Merging two lists into one

開始 arrays

listone[1,2,3,4,5]
listtwo[a,b,c,d,e]

想要的結果

[1a,2b,3c,4d,5e]

我的嘗試

filename = open('Forks.csv', 'r')
file = csv.DictReader(filename)
Item =  []
Price = []
Full = []
for col in file:
    Item.append(col['Item Name'])
    Price.append(col['Price'])
for n in Item and Price:
    Full_Var = (n[Item],':', '£', n[Price])
    Full.append(Full_Var)

它來自帶有兩個列(Item_Name 和 Price)的 csv 文件

您可以像這樣輕松合並兩個列表:

list_one = [1, 2, 3, 4, 5]
list_two = ["a", "b", "c", "d", "e"]

merged_list = []
for i, j in zip(list_one, list_two):
    merged_list.append(str(i) + str(j))
print(merged_list)

這是合並兩個列表的解決方案。

l1 = [1,2,3,4]
l2 = ['a','b','c','d']
res = []
for i in range(len(l1)):
    res.append(str(l1[i])+l2[i])
    
print(res)

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM