Skip to main content
 首页 » 编程设计

python之每次遇到某个字符 (,) 时添加字符 (',') python 2.7.3

2025年05月04日20bluestorm

假设你有一个字符串

test = 'wow, hello, how, are, you, doing' 

你想要

full_list = ['wow','hello','how','are','you','doing'] 

我知道你会从一个空列表开始:

empty_list = [] 

并会创建一个 for 循环以将项目附加到列表中

我只是对如何去做感到困惑,

我正在尝试以下方法:

for i in test: 
    if i == ',': 

然后我卡住了。 . .

请您参考如下方法:

在 Python 中,做你想做的最好的方法是

full_list = test.split(', ') 

如果您的字符串可能有一些逗号后面没有空格,您需要做一些更稳健的事情。也许

full_list = [x.lstrip() for x in test.split(',')]