Python将字符串列表还原为字符串

问题描述 投票:0回答:1

我目前正在处理一个问题,我需要将一个字符串列表缩减为一个单一的字符串,对每个字符串稍作修改。例如,给定输入["apple", "pear", peach"],我想要 "apple0 pear0 peach0 "作为输出。

用我使用的reduce函数,我得到的输出是 "apple0 pear0 peach0"。

reduce(lambda x,y: x + "0 " + y, string_list)

我得到的输出是 "apple0 pear0 peach", 没有修改输入列表中的最后一个元素. 我想解决这个问题,使我的最后一个元素也得到修改。

python functional-programming anonymous-function
1个回答
3
投票

考虑到 l 是你的名单与 join

' '.join(map(lambda x : x+'0',l))
'apple0 pear0 peach0'

'0 '.join(l)+'0'
'apple0 pear0 peach0'

基于@Bobby的评论

' '.join(x+'0' for x in l) 
© www.soinside.com 2019 - 2024. All rights reserved.