Reputation: 1974
This code works but I was wondering if there is a more pythonic way for writing it.
word_frequency
is a dictionary of lists, e.g.:
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
Is there a more elegant way of writing this loop?
Upvotes: 5
Views: 205
Reputation: 73
for n, p in your_dict.vales():
res[0] += n
res[1] += p
This will be fast and elegant enough. Sent from phone. Sorry for the format.
Upvotes: 0
Reputation: 2159
Try This Single Line Solution:
[sum([word_frequency[i][0] for i in word_frequency]),sum([word_frequency[i][1] for i in word_frequency])]
Upvotes: 1
Reputation: 1322
Probably not the shortest way to solve this, but hopefully the most comprehensible...
word_frequency = {'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]}
negative = (v[0] for v in word_frequency.values())
positive = (v[1] for v in word_frequency.values())
vocab_frequency = sum(negative), sum(positive)
print (vocab_frequency) # (15622, 7555)
Though more experienced Pythonistas might rather use zip to unpack the values:
negative, positive = zip(*word_frequency.values())
vocab_frequency = sum(negative), sum(positive)
Upvotes: 2
Reputation: 162
You can convert that dictionary to a pandas DataFrame and it would be a lot easier to handle.
import pandas as pd
word_frequency = {'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]}
#Syntax to create DataFrame
df = pd.DataFrame(word_frequency)
#Result
dogs are fun
0 1234 9999 4389
1 4321 0 3234
Now just take the sum of each row and either convert back to list or keep as a dataframe object.
#Take sum of each row and convert to list
df = df.sum(axis=1)
df = df.values.tolist()
print(df)
#Output
[15622, 7555]
Upvotes: 1
Reputation: 7844
Another approach would be this:
vocab_frequency[0], vocab_frequency[1] = list(sum([word_frequency[elem][i] for elem in word_frequency]) for i in range(2))
print(vocab_frequency[0])
print(vocab_frequency[1])
Output:
15622
7555
Still, one more way to do it, kind of far fetched is this:
*vocab_frequency, = list(map(sum,zip(*word_frequency.values())))
print(vocab_frequency)
Output:
[15622, 7555]
Upvotes: 1
Reputation: 5822
for frequencies in word_frequency.values():
vocab_frequency = [sum(x) for x in zip(vocab_frequency, frequencies)]
Upvotes: 1
Reputation: 438
You could use numpy for that:
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)
Upvotes: 4
Reputation: 12930
I'm not sure if this is more 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]
Alternate solution with reduce
:
>>> reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], word_frequency.values())
[15622, 7555]
Upvotes: 8