简单添加一个字符串和一个字符串列表将产生错误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]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句