这段代码有效,但我想知道是否有更多的pythonic方式来编写它.
word_frequency
是一个列表字典,例如:
word_frequency = {'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]}
vocab_frequency = [0, 0] # stores the total times all the words used in each class
for word in word_frequency: # that is not the most elegant solution, but it works!
vocab_frequency[0] += word_frequency[word][0] #negative class
vocab_frequency[1] += word_frequency[word][1] #positive class
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方式来编写这个循环?
我不确定这是否更像Pythonic:
>>> word_frequency = {'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]}
>>> vocab_frequency = [sum(x[0] for x in word_frequency.values()),
sum(x[1] for x in word_frequency.values())]
>>> print(vocab_frequency)
[15622, 7555]
Run Code Online (Sandbox Code Playgroud)
替代解决方案reduce
:
>>> reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], word_frequency.values())
[15622, 7555]
Run Code Online (Sandbox Code Playgroud)
你可以使用 numpy 来实现:
import numpy as np
word_frequency = {'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]}
vocab_frequency = np.sum(list(word_frequency.values()), axis=0)
Run Code Online (Sandbox Code Playgroud)