![](/img/trans.png)
[英]How remove all non-alphabet chars excluding white space from string in Python without joining the words
[英]Python : find words in string without white space
我试图做一个函数来查找字符串中没有空格的单词:'Daysaregood'。 我对每个字母进行迭代,直到通过使用附魔模块附魔将其与基于已迭代字母的列表进行比较来找到单词是否存在。 这就是我尝试过的:
import enchant
import time
fulltext =[]
def work(out):
if len(out)>0:
word = ''
wd = ""
# iterate for every Letter
for i in out:
word = word + i
print word
d = enchant.Dict('en_US')
# a list of words to compare to
list = d.suggest(word.title())
print list
#check if word exists
if word.title() in list :
print 'Word found'
wd = word
else:
print 'Word not found'
print '\n'+wd
fulltext.append(str(wd))
time.sleep(2)
work(out[len(wd):])
else:
print '\n fulltext : '
print fulltext
word="Daysaregood"
work(word)
现在,对于此文本,脚本可以按我的意愿运行,我得到如下列表:['Days','are','good']。 但是,当我尝试使用“ spaceshuttle”之类的功能时,该函数会与“ space”混淆,并将“ s”换成“ shuttle”中的“ s”,因此我得到以下信息:['spaces,'hut','t','l', 'e']。 我的目标是使每个单词单独返回,并将它们存储在列表中。 任何帮助表示赞赏。
您的任务的问题在于,期望的输出本身并不遵循严格的规则。 如果要输入'pineapple'
,您会期望['pine', 'apple']
或['pineapple']
吗? 很难预测这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.