Python,两个lis的连接

2024-09-23 06:30:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,我需要这样做:

lines = ['apple','bear']
signed=['aelpp','aber']

我希望输出为:

res = ['aelpp apple', 'aber bear']

如果你能帮忙,我将不胜感激!我试过简单地使用+和join()函数,但没有得到我想要的。你知道吗


Tags: 函数applereslinessignedbearjoin新手
3条回答

您可以使用mapzip

list(map(lambda x: x[1] + ' ' + x[0], zip(lines, signed)))

由于您是python新手,与其他人相比,您可能会发现以下内容更容易理解:

>>> res = []
>>> for i in range(len(signed)):
...     res.append(signed[i] + ' ' + lines[i])

结果是:

>>> print res
['aelpp apple', 'aber bear']

您可以尝试使用^{}join()

res = [" ".join(e) for e in zip(signed, lines)]
print res

输出:

['aelpp apple', 'aber bear']

编辑:正如@ThiefMaster评论的那样,可以使用map()使其更加紧凑:

res = map(' '.join, zip(signed, lines))

相关问题 更多 >