如何从字符串中排序所有可能的单词?

如何从字符串中排序所有可能的单词?,第1张

如何从字符串中排序所有可能的单词

另一种可能性是反其道而行之,而不是从字符串中生成子字符串,而是抓住所有候选单词并使它们与您的字符串匹配

您可以将原始字符串中单词的索引对(开始,结束)存储为结果。

这可以在正则表达式中轻松完成,或者如果使用str.find()不够出色,或者即使使用更复杂的字典索引方案或关于什么可以匹配和不匹配的聪明方法,也可以轻松完成(请参见Gregg的答案以获取想法)

这里有我的意思的样本

candidate = "thingsandstuffmydarlingpretty"words = file('/usr/share/dict/words').read()#This generator calls find twice, it should be rewritten as a normal loopgenerate_matches = ((candidate.find(word),word) for word in words.split('n')          if candidate.find(word) != -1 and word != '')for match in generate_matches:    print "Found %s at (%d,%d)" % (match[1],match[0],match[0] + len(match[1]))


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5668522.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存