AssMero
AssMero

Reputation: 249

Python dictionary operation

I have something like the following dictionary:

dict = {}
dict[(-1,"first")]=3
dict[(-1,"second")]=1
dict[(-1,"third")]=5
dict[(0,"first")]=4
dict[(0,"second")]=6
dict[(0,"third")]=7
dict[(1,"first")]=34
dict[(1,"second")]=45
dict[(1,"third")]=66
dict[(2,"first")]=3
dict[(2,"second")]=1
dict[(2,"third")]=2

What I would like now is a dict with the following structure: Keys are "first" "second" "third", values are the numbers --> start: if first entry in tuple > 0

dict_1 ={"first": [4,34,3], "second": [6,45,1], "third": [7,66,2]}

I tried it with:

for key, value in dict.iteritems():
   if key[0] <=0:
..
..

But that changes the order and does not work really properly. Would be great if anyone would suggest a simple method to handle such things.

Thank you very much

Upvotes: 3

Views: 109

Answers (4)

lionel319
lionel319

Reputation: 1256

    t = {}
    t[(-1,"first")]=3
    t[(-1,"second")]=1
    t[(-1,"third")]=5
    t[(0,"first")]=4
    t[(0,"second")]=6
    t[(0,"third")]=7
    t[(1,"first")]=34
    t[(1,"second")]=45
    t[(1,"third")]=66
    t[(2,"first")]=3
    t[(2,"second")]=1
    t[(2,"third")]=2
    
    new = {}
    for k in sorted(t.keys()):
        if k[0]>-1:
            if k[1] not in new:
                new[k[1]] = []
            new[k[1]].append(t[k])

    from pprint import pprint
    pprint(new)
    # {'first': [4, 34, 3], 'second': [6, 45, 1], 'third': [7, 66, 2]}


Upvotes: 0

Ohad Eytan
Ohad Eytan

Reputation: 8464

I will do something like that using defaultdict for convenience:

from collections import defaultdict
new_dict = defaultdict(list)

for (x,k),v in sorted(old_dict.items()): # iterating over the sorted dictionary 
    if x >= 0:
        new_dict[k].append(v)

dict(new_dict)
#output:
{'second': [6, 45, 1], 'first': [4, 34, 3], 'third': [7, 66, 2]}

BTW, don't name your dictionary dict, it's shadowing python dict type.

Upvotes: 1

lionel319
lionel319

Reputation: 1256

dict = {}
dict[(-1,"first")]=3
dict[(-1,"second")]=1
dict[(-1,"third")]=5
dict[(0,"first")]=4
dict[(0,"second")]=6
dict[(0,"third")]=7
dict[(1,"first")]=34
dict[(1,"second")]=45
dict[(1,"third")]=66
dict[(2,"first")]=3
dict[(2,"second")]=1
dict[(2,"third")]=2

dict_1 = {}
for num,txt in sorted(dict.keys()):
    if num >= 0:
        val = dict[(num,txt)]
        if txt in dict_1:
            dict_1[txt].append(val)
        else:
            dict_1[txt] = [val]

print dict_1

Upvotes: 0

Quentin
Quentin

Reputation: 174

Why do you want to keep the order ? I suggest you to use this kind of loop

dict_r = {}
dict_r["first"] = []
dict_r["second"] = []
dict_r["third"] = []
for i in range (0,3):
    dict_r["first"].append(dict[i,"first"])
    dict_r["second"].append(dict[i,"second"])
    dict_r["third"].append(dict[i,"third"])

Update

if you don't know how many items are in the dict

dict_r = {}
dict_r["first"] = []
dict_r["second"] = []
dict_r["third"] = []


for key, value in dict.iteritems():
   if key[0] <=0:
       dict_r[key[1]].append(value)

Upvotes: 2

Related Questions