我想挂载一个数据结构,说明发生的数量和映射他们在正确的顺序。你知道吗
例如:
word_1 => 10 occurences
word_2 => 5 occurences
word_3 => 12 occurences
word_4 => 2 ocurrences
每个单词都有一个id来表示它:
kw2id = ['word_1':0, 'word_2':1, 'word_3':2, 'word_4': 3]
所以一个有序的列表应该是:
ordered_vocab = [2, 0, 1, 3]
例如,我的代码是…:
#build a vocabulary with the number of ocorrences
vocab = {}
count = 0
for line in open(DATASET_FILE):
for word in line.split():
if word in vocab:
vocab[word] += 1
else:
vocab[word] = 1
count += 1
if not count % 100000:
print(count, "documents processed")
如何有效地执行此操作?你知道吗
这就是^{} 的用途:
或者(使用发电机时更短更“漂亮”):
你可以用收款台. 计数器允许您输入一个列表,它将自动计算每个元素的出现次数。你知道吗
因此,除了上面的答案之外,您可以做的是从文件中创建一个单词列表,并使用Counter和一个列表,而不是手动遍历列表中的每个元素。请注意,如果文件与内存相比太大,则此方法不适用。你知道吗
这是一个稍微快一点的代码版本,很抱歉我不太了解numpy,但也许这会有所帮助,
enumerate
和defaultdict(int)
是我所做的更改(你不必接受这个答案,只是想帮忙)另外,对于for循环(运行Python 3.44)中的增量,从0开始时的
defaultdict(int)
似乎是Counter()
的两倍:结果:
如果你想对这项索赔提出异议,我请你回答这个问题:Surprising results with Python timeit: Counter() vs defaultdict() vs dict()
相关问题 更多 >
编程相关推荐