简体   繁体   English

如何在2个列表python 3之间提取列表?

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

How to map and append values from one list to other 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"]

You can concatenate the strings in the sublists together while iterating over the two lists together using zip , stripping the individual strings to get rid of surrounding whitespaces in the process 您可以将子列表中的字符串连接在一起,同时使用zip遍历两个列表,同时剥离单个字符串以消除过程中周围的空白

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

To do it without zip, we would need to explicitly use indexes to access elements in the list 要在没有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)

The output will be 输出将是

['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