在 python 列表中搜索匹配不同长度词干的自定义列表

Search a python list for matches to a custom list of stem words of varying length

我正在尝试使用 python 搜索自定义词干词的词标记化摘要。下面的代码几乎就是我想要的。也就是说,stem_words 中的任何值是否在 word_tokenized_abstract 中出现一次或多次?

if(any(word in stem_words for word in word_tokenized_abstract)):
    do stuff

其中...

我根据以上 one-liner to check if at least one item in list exists in another list?

我的问题是我的 stem_words 长度不同。我已经尝试了以下代码(对上述代码的修改),但对我不起作用。我尝试了其他一些修改,但它们要么不起作用,要么导致崩溃。

if(any(word in stem_words for word[0:len(word)] in word_tokenized_abstract)):
    do stuff

也就是说,任何值 word_tokenized_abstract 是否以 stem_words 中的任何值开头?

如果有帮助,我的 stem_words = ['pancrea', 'muscul', 'derma', 'ovar']

谢谢!如果之前已经回答过这个问题,但我找不到它,我深表歉意。

所以你想检查第一个列表中的任何字符串是否包含在第二个列表的任何字符串中。

我会试试这个:

any(y.startswith(x) for y in word_tokenized_abstract for x in stem_words)

解释:对于 stem_words 中的每个词干 x 检查 word_tokenized_abstract 中是否有任何字符串以 x.

开头

如果您只想将词干作为单词的子串,请使用:

any(x in y for y in word_tokenized_abstract for x in stem_words)