这段代码是可行的,但我想知道是否有更符合Python语言特点的写法。
有没有更优雅的方式来编写这个循环呢?
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
有没有更优雅的方式来编写这个循环呢?
word_frequency
回溯word
,那么可以直接迭代word_frequency.values()
。 - pdexter