Fastest way to check if a item is in a list - Python
本问题已经有最佳答案,请猛点这里访问。
我在用Python编写词汇表时遇到问题。我的代码在大约2.3MB的文档中遍历每个单词,并检查该单词是否在字典中,如果不在字典中,则将其附加到列表中。
问题是,这需要很长时间(我甚至还没有完成)。我怎么解决这个问题?
代码:
1 2 3 4 5 | 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) |
除非您需要
1 | vocab = set(words) |
以下是比较for循环和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import random import time import string words = [''.join(random.sample(string.letters, 5)) for i in range(1000)]*10 # *10 to make duplicates! vocab1 = [] t1 = time.time() for w in words: if w not in vocab1: vocab1.append(w) t2 = time.time() t3 = time.time() vocab2 = set(words) t4 = time.time() print t2 - t1 print t4 - t3 |
输出:
1 2 | 0.0880000591278 # Using for loop 0.000999927520752 # Using set() |