0

I am dealing with very large three dictionaries which looks like this:

dict_a = { ( 't','e' ) : [0.5,0.1,0.6],  ( 'a','b' ) : [0.2,0.3,0.9] }

dict_b = { ( 'a','b' ) : [0.1,0.5,0.3] , ( 't','e' ) : [0.6,0.1,0.6] }

dict_c = { ( 'a','b' ) : [0.1,0.5,0.3] , ( 't','e' ) : [0.6,0.5,0.6] }

I am looking for the output like this :

    name    first_value       second_value  third_value

0   (t, e)  [0.5, 0.1, 0.6] [0.6, 0.1, 0.6] [0.6, 0.5, 0.6]
1   (a, b)  [0.2, 0.3, 0.9] [0.1, 0.5, 0.3] [0.1, 0.5, 0.3]

What I've tried is :

final_dict = {'name': [] , 'first_value' : [] ,'second_value': [] , 'third_value': [] }

for a,b in dict_a.items():
    for c,d in dict_b.items():
        for e,f in dict_c.items():
            if a==c==e:
                final_dict['name'].append(a)
                final_dict['first_value'].append(b)
                final_dict['second_value'].append(d)
                final_dict['third_value'].append(f)

Which is really not efficient and optimize way to do this task. I was thinking to use pandas.

How can I do this task in minimal time complexity?

Thank you !

Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
  • This gets you all the keys from all the dicts, so you only need one loop. O(n) + O(n) = O(n) https://stackoverflow.com/questions/5242311/python-union-keys-from-multiple-dictionary – Kenny Ostrom Jul 25 '19 at 14:25

4 Answers4

4

Because these are dictionaries, you only need to iterate over one. You can use the key to get the corresponding value from the others.

Example:

for key, value in dict_a.items():
        final_dict['name'].append(key)
        final_dict['first_value'].append(value)
        final_dict['second_value'].append(dict_b[key])
        final_dict['third_value'].append(dict_c[key])
chepner
  • 497,756
  • 71
  • 530
  • 681
Kendas
  • 1,963
  • 13
  • 20
  • 1
    For readability, I might just use `for key_in dict_a`. Then the `final_dict` assignments (aside from the first) can all be written uniformly as `final_dict['...'].append(...[key])`. – chepner Jul 25 '19 at 14:02
  • (Might be good to confirm, too, that all three dictionaries have the same set of keys.) – chepner Jul 25 '19 at 14:03
  • I was thinking set union of the keys, then iterate over keys to build the output -- just in case some dict happens to be missing some entry. – Kenny Ostrom Jul 25 '19 at 14:09
1

Try this way:-

df = pd.DataFrame([dict_a, dict_b, dict_c], index = ['first_value', 
'second_value', 'third_value']).T
df['names'] = df.index
df.index = [0, 1]
print(df)

Output:-

       first_value     second_value      third_value   names
0  [0.2, 0.3, 0.9]  [0.1, 0.5, 0.3]  [0.1, 0.5, 0.3]  (a, b)
1  [0.5, 0.1, 0.6]  [0.6, 0.1, 0.6]  [0.6, 0.5, 0.6]  (t, e)
Vicrobot
  • 3,795
  • 1
  • 17
  • 31
1

How about:

pd.DataFrame({i:d for i,d in enumerate([dict_a,dict_b,dict_c])} )

Output:

                   0                1                2
a b  [0.2, 0.3, 0.9]  [0.1, 0.5, 0.3]  [0.1, 0.5, 0.3]
t e  [0.5, 0.1, 0.6]  [0.6, 0.1, 0.6]  [0.6, 0.5, 0.6]
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
1

Here is one way

pd.concat([pd.Series(x) for x in [dict_a,dict_b,dict_c]],axis=1)
Out[332]: 
                   0                1                2
a b  [0.2, 0.3, 0.9]  [0.1, 0.5, 0.3]  [0.1, 0.5, 0.3]
t e  [0.5, 0.1, 0.6]  [0.6, 0.1, 0.6]  [0.6, 0.5, 0.6]
BENY
  • 317,841
  • 20
  • 164
  • 234