Skip to main content
 首页 » 编程设计

python之向列表中的每个单独的字符串添加一个字符串

2025年05月04日23xxx_UU

简单地添加一个字符串和一个字符串列表会产生错误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"] 

请您参考如下方法:

使用理解:

list_of_strings = [s + string_to_add for s in list_of_strings]