如何将值从一个列表映射并附加到另一个列表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']

关于python - 如何在2个列表python 3之间提取列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56579862/

10-09 17:14
查看更多