将字符串添加到列表中的每个单独的字符串

时间:2014-09-01 11:33:52

标签: python string list

简单地添加字符串和字符串列表会产生错误cannot concatenate 'str' and 'list' objects。是否有更优雅的方式来执行以下操作(例如,没有循环)?

list_of_strings = ["Hello", "World"]
string_to_add = "Foo"

for item, string in enumerate(list_of_strings):
    list_of_strings[item] = string_to_add + string

# list_of_strings is now ["FooHello", "FooWorld"]

2 个答案:

答案 0 :(得分:6)

使用理解:

list_of_strings = [s + string_to_add for s in list_of_strings]

答案 1 :(得分:2)

尝试使用地图

list_of_strings = ["Hello", "World"]
string_to_add = "Foo"


print map(lambda x:string_to_add+x,list_of_strings)