我有一张字典里的单词列表。我想找到一种删除所有单词的方法,只考虑在目标单词的开头处形成的根单词。
例如,单词"rodeo“将从列表中删除,因为它包含英文有效的单词”rodeo“。“打字机”将被删除,因为它包含英语有效的单词“类型”。然而,即使包含"nick“一词,"snicker”这个词仍然有效,因为"nick“位于单词的中间,而不是在单词的开头。
我在想这样的事情:
for line in wordlist:
if line.find(...) --但是,我希望" if“语句能够遍历列表中的每个单词,检查它是否找到了,如果是的话,从列表中删除自己,这样就只剩下根单词了。我是否必须创建一个文字列表的副本来遍历?
发布于 2011-01-22 04:55:24
我假设您只有一个列表,您希望从其中删除在同一列表中有前缀的任何元素。
#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编辑:添加了一些注释,以使逻辑更加明显。
发布于 2011-01-22 05:41:07
我发现jkerian是最好的(假设只有一个列表),我想解释一下原因。
下面是我的代码版本(作为一个函数):
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);只要对单词列表进行排序(如果您愿意,可以在函数中这样做),这将在一个解析中得到结果。这是因为当您对列表进行排序时,列表中由另一个单词组成的所有单词都将直接排在该根单词之后。例如,在你的特定列表中,任何介于“弧形”和“神秘”之间的东西,也会因为“弧形”这个词根而被删除。
发布于 2011-01-22 06:50:12
为此,应该使用内置的lambda函数。我想这会让你的生活轻松得多
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)除非我误解了你的问题,否则这应该符合你的目的。
希望这能有所帮助
https://stackoverflow.com/questions/4766157
复制相似问题