我是新来的蟒蛇,我需要做串联:Python中,两个列表
lines = ['apple','bear']
signed=['aelpp','aber']
我所要的输出是:
res = ['aelpp apple', 'aber bear']
我会很感激,如果你能帮助!我试着简单地使用+和join()函数,但没有得到我想要的。
我是新来的蟒蛇,我需要做串联:Python中,两个列表
lines = ['apple','bear']
signed=['aelpp','aber']
我所要的输出是:
res = ['aelpp apple', 'aber bear']
我会很感激,如果你能帮助!我试着简单地使用+和join()函数,但没有得到我想要的。
您可以尝试使用zip()
和join()
:
res = [" ".join(e) for e in zip(signed, lines)]
print res
输出:
['aelpp apple', 'aber bear']
编辑:作为@ThiefMaster评论说,这可以使用map()
更加紧凑:
res = map(' '.join, zip(signed, lines))
您可以使用map
和zip
:
list(map(lambda x: x[1] + ' ' + x[0], zip(lines, signed)))
'map(lambda ...'总是更好地写成列表理解/生成器表达式。 –
既然你是新的蟒蛇,你会发现下面的更容易比别人理解:
>>> res = []
>>> for i in range(len(signed)):
... res.append(signed[i] + ' ' + lines[i])
结果:
>>> print res
['aelpp apple', 'aber bear']
可以用更紧凑的方式完成:'map(''.join,zip(signed,lines))' – ThiefMaster
谢谢很多!这正是我期待的! –