Skip to main content
 首页 » 编程设计

python之检查项目是否在列表中的最快方法之Python

2025年05月04日23leader

<分区>

我在用 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)