This question already has answers here:
Fastest way to check if a value exists in a list
(14个回答)
2年前关闭。
我在用python制作词汇表时遇到了问题。我的代码遍历了约2.3MB的文档中的每个单词,并检查单词是否在字典中,如果不是,它将附加到列表中
问题是,它要花很长时间(我什至还没有完成它)。我该如何解决?
码:
(14个回答)
2年前关闭。
我在用python制作词汇表时遇到了问题。我的代码遍历了约2.3MB的文档中的每个单词,并检查单词是否在字典中,如果不是,它将附加到列表中
问题是,它要花很长时间(我什至还没有完成它)。我该如何解决?
码:
words = [("_", "hello"), ("hello", "world"), ("world", "."), (".", "_")] # List of a ton of tuples of words
vocab = []
for w in words:
if not w in vocab:
vocab.append(w)
最佳答案
除非您需要vocab
进行特定订购,否则您可以执行以下操作:
vocab = set(words)
关于python - 检查项目是否在列表中的最快方法-Python ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41337529/