簡體   English   中英

如何在2個列表python 3之間提取列表?

[英]How to extract list between 2 list python 3?

如何將值從一個列表映射並附加到另一列表python 3?

in_put_1 = [["a alphanum2 c d"], ["g h"]] 
in_put_2 = [["e f"], [" i j k"]]

output = ["a alphanum2 c d e f", "g h i j k"]

您可以將子列表中的字符串連接在一起,同時使用zip遍歷兩個列表,同時剝離單個字符串以消除過程中周圍的空白

[f'{x[0].strip()} {y[0].strip()}' for x, y in zip(in_put_1, in_put_2)]

要在沒有zip的情況下執行此操作,我們需要顯式使用索引來訪問列表中的元素

result = []
for idx in range(len(in_put_1)):

    s = f'{in_put_1[idx][0].strip()} {in_put_2[idx][0].strip()}'
    result.append(s)

輸出將是

['a alphanum2 c d e f', 'g h i j k']
>>>map(lambda x,y: x[0]+" "+y[0],in_put_1,in_put_2)
['a alphanum2 c d e f', 'g h  i j k']
[' '.join(element1+element2) for (element1,element2) in zip(in_put_1,in_put_2) ]
a = [["a alphanum2 c d"], ["g h"]] 
b = [["e f"], [" i j k"]]
c = []
for (a1, b1) in zip(a,b): 
     c.append(''.join([str(a) + b for a,b in zip(a1,b1)]))
print(c)

暫無
暫無

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

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