我想用逗号‘,’,句号‘’和空格‘’,不使用任何内置的拆分函数,并将这些单词追加到列表中.
输入: 嗨,欢迎来到MK培训.我喜欢Python .
预期输出: [‘嗨’,‘欢迎’,‘来’,‘MK’,‘培训’,‘我’,‘爱’,‘Python ’]
以下是我try 过的代码:
s = "Hi, Welcome to MK training. I love python."
c_pos = -1
last_pos = - 1
r = []
for i in s[c_pos+1:]:
if i == ' ' or i == ',' or i == '.':
c_pos = s.index(i, c_pos+1)
r.append(s[last_pos+1:c_pos])
last_pos = c_pos
print(r)
输出: [‘嗨’,‘’,‘欢迎’,‘to’,‘MK’,‘培训’,‘’,‘我’,‘爱’,‘Python ’]
我知道如果我使用List Remove()方法,我可以从List中删除‘’,并且我会得到我预期的输出.我想要的是,做所有的操作与字符串和拆分逗号,句号和空格从字符串不使用任何拆分函数,并附加在一个列表中的这些单词.我不想对List做任何事情.