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

标签 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"]

最佳答案

使用理解:

list_of_strings = [s + string_to_add for s in list_of_strings]

关于python - 向列表中的每个单独的字符串添加一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25604949/

相关文章:

python - Python 的 IMAP 库支持哪些线程算法?

用于评估随机游走的python库?

python - 从python中的2个列表中删除不匹配的项目

python - Elastic Beanstalk ;配置域名的正确别名目标是什么?

python - 求和()在 python

c++ - 使用模板处理整数、字符串和字符串数组

string - Elixir 只大写一个单词的第一个字母

c# - 用于识别后跟数字的 Guid 的正则表达式

list - 删除 Redis 列表中的所有条目

sql - T-SQL,制作列表