【发布时间】:2021-07-23 03:55:06
【问题描述】:
这是我的代码
def words_only(sentence):
wordlist1 = sentence.split()
wordlist2 = []
for word in wordlist1:
modified = ''
for char in word:
if char in '_-!,.?":;0123456789':
char = ''
modified += char
wordlist2.append(modified)
return wordlist2
描述是:words_only 接收一个字符串作为参数,并返回一个包含句子中所有单词的列表。出于此功能的目的,单词是仅由字母组成的序列(小写或大写 输入是
words_only("two-fold will count as 2 words.")
但是,我上次测试失败了。我的输出是
['twofold', 'will', 'count', 'as', '', 'words']
正确的输出应该是
["two", "fold", "will", "count", "as", "words"]
我怎样才能修复我的代码,使冒号消失并且“双倍”将计为 2 个单词?并且还有一个空字符串导致了错误。
【问题讨论】:
-
请在此处粘贴代码
-
请edit问题并粘贴代码
标签: python python-3.x string list