首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python-删除列表中包含其他单词的所有单词

Python-删除列表中包含其他单词的所有单词
EN

Stack Overflow用户
提问于 2011-01-22 04:23:31
回答 6查看 6.2K关注 0票数 3

我有一张字典里的单词列表。我想找到一种删除所有单词的方法,只考虑在目标单词的开头处形成的根单词。

例如,单词"rodeo“将从列表中删除,因为它包含英文有效的单词”rodeo“。“打字机”将被删除,因为它包含英语有效的单词“类型”。然而,即使包含"nick“一词,"snicker”这个词仍然有效,因为"nick“位于单词的中间,而不是在单词的开头。

我在想这样的事情:

代码语言:javascript
运行
复制
 for line in wordlist:
        if line.find(...) --

但是,我希望" if“语句能够遍历列表中的每个单词,检查它是否找到了,如果是的话,从列表中删除自己,这样就只剩下根单词了。我是否必须创建一个文字列表的副本来遍历?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-01-22 04:55:24

我假设您只有一个列表,您希望从其中删除在同一列表中有前缀的任何元素。

代码语言:javascript
运行
复制
#Important assumption here... wordlist is sorted

base=wordlist[0]                      #consider the first word in the list
for word in wordlist:                 #loop through the entire list checking if
    if not word.startswith(base):     # the word we're considering starts with the base
        print base                    #If not... we have a new base, print the current
        base=word                     #  one and move to this new one
    #else word starts with base
        #don't output word, and go on to the next item in the list
print base                            #finish by printing the last base

编辑:添加了一些注释,以使逻辑更加明显。

票数 5
EN

Stack Overflow用户

发布于 2011-01-22 05:41:07

我发现jkerian是最好的(假设只有一个列表),我想解释一下原因。

下面是我的代码版本(作为一个函数):

代码语言:javascript
运行
复制
wordlist = ["a","arc","arcane","apple","car","carpenter","cat","zebra"];

def root_words(wordlist):
    result = []
    base = wordlist[0]
    for word in wordlist:
        if not word.startswith(base):
            result.append(base)
            base=word
    result.append(base)
    return result;

print root_words(wordlist);

只要对单词列表进行排序(如果您愿意,可以在函数中这样做),这将在一个解析中得到结果。这是因为当您对列表进行排序时,列表中由另一个单词组成的所有单词都将直接排在该根单词之后。例如,在你的特定列表中,任何介于“弧形”和“神秘”之间的东西,也会因为“弧形”这个词根而被删除。

票数 1
EN

Stack Overflow用户

发布于 2011-01-22 06:50:12

为此,应该使用内置的lambda函数。我想这会让你的生活轻松得多

代码语言:javascript
运行
复制
words = ['rode', 'nick'] # this is the list of all the words that you have.
                         # I'm using 'rode' and 'nick' as they're in your example
listOfWordsToTry = ['rodeo', 'snicker']
def validate(w):
    for word in words:
        if w.startswith(word):
            return False
    return True

wordsThatDontStartWithValidEnglishWords = \
    filter(lambda x : validate(x), listOfWordsToTry)

除非我误解了你的问题,否则这应该符合你的目的。

希望这能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4766157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档