【发布时间】:2019-04-24 09:49:50
【问题描述】:
我需要创建一个函数,将字符串转换为不带!?., %#$ . 且不带大写字母的列表。最后的字符串只是一个例子,所以它需要返回['mr', 'stark', 'i', "don't", 'feel', 'so', 'good']
谁能告诉我为什么我的代码打印None?
def sentence_to_words(s):
# Write the rest of the code for question 2 below here.
s_new= []
s1 = s.split()
a = ['#',',','!','.','?','$']
for i in s.split():
if i in a:
s2 = s1.remove(i)
s_new = s_new.append(s2)
return s_new
print sentence_to_words("Mr. Stark... I don't feel so good")
【问题讨论】:
-
你的方法有什么问题?
-
它不会在列表中的任何值上运行.. 它只是不打印
-
我认为您的
return不应该在if语句中。 -
在
for i in s.split():中,您正在检查单词的每个单词并查看它是否在a中。您应该查看a的元素是否在s中。
标签: string python-2.7 list split append