【发布时间】:2017-05-11 06:56:46
【问题描述】:
我在用 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)
【问题讨论】:
-
你有多少字?为什么不使用
set()而不是列表? -
您能否提供您正在检查的单词的副本。
-
words 实际上是一个元组列表(n-gram)
标签: python python-2.7 list python-3.x