Victor Zuanazzi
Victor Zuanazzi

Reputation: 1974

Pythonic way of executing a loop on a dictionary of lists

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

Answers (9)

fakeKmz
fakeKmz

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

Rohit-Pandey
Rohit-Pandey

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

Sebastian Loehner
Sebastian Loehner

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

Drew Nicolette
Drew Nicolette

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

Vasilis G.
Vasilis G.

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

drhodes
drhodes

Reputation: 1009

list(map(sum, zip(*word_frequency.values())))

Upvotes: 2

Karl
Karl

Reputation: 5822

for frequencies in word_frequency.values():
    vocab_frequency = [sum(x) for x in zip(vocab_frequency, frequencies)] 

Upvotes: 1

David Speck
David Speck

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

hochl
hochl

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

Related Questions