Skip to main content
 首页 » 编程设计

python之如何在列表的每个元素的末尾添加一个单词

2025年05月04日9lyhabc

我有 list :

mylist = ["all dogs go", "why does one", "fell down so hard I"] 

是否有一个函数可以让我在 mylist 中的每个元素的末尾添加单词 “moo”?

请您参考如下方法:

使用列表理解:

>>> mylist[:] = [word.strip() + " moo" for word in mylist] 
>>> print mylist 
['all dogs go moo', 'why does one moo', 'fell down so hard I moo']