2024-09-23 06:30:54 发布
网友
我是python新手,我需要这样做:
lines = ['apple','bear'] signed=['aelpp','aber']
我希望输出为:
res = ['aelpp apple', 'aber bear']
如果你能帮忙,我将不胜感激!我试过简单地使用+和join()函数,但没有得到我想要的。你知道吗
您可以使用map和zip:
map
zip
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():
join()
res = [" ".join(e) for e in zip(signed, lines)] print res
输出:
['aelpp apple', 'aber bear']
编辑:正如@ThiefMaster评论的那样,可以使用map()使其更加紧凑:
map()
res = map(' '.join, zip(signed, lines))
您可以使用
map
和zip
:由于您是python新手,与其他人相比,您可能会发现以下内容更容易理解:
结果是:
您可以尝试使用^{} 和
join()
:输出:
编辑:正如@ThiefMaster评论的那样,可以使用
map()
使其更加紧凑:相关问题 更多 >
编程相关推荐