如何将值从一个列表映射并附加到另一列表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"]
Run Code Online (Sandbox Code Playgroud)
您可以将子列表中的字符串连接在一起,同时使用zip遍历两个列表,同时剥离单个字符串以消除过程中周围的空白
[f'{x[0].strip()} {y[0].strip()}' for x, y in zip(in_put_1, in_put_2)]
Run Code Online (Sandbox Code Playgroud)
要在没有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)
Run Code Online (Sandbox Code Playgroud)
输出将是
['a alphanum2 c d e f', 'g h i j k']
Run Code Online (Sandbox Code Playgroud)