sklearning
sklearning

Reputation: 223

Sort inter-dependant elements of a dictionary

I have a dictionary like this

dict = {'name':''xyz','c1':['a','b','r','d','c'],'c2':['21','232','11','212','34']}

Here dict.c1 values and dict.c2 values are inter-dependant. That is, 'a' is related to '21', 'd' is related to '212', 'b' is related to '232'... I have to sort c1 and c2 should get reflected accordingly. The final output should be

dict = {'name':''xyz','c1':['a','b','c','d','r'],'c2':['21','232','34','212','11']}

What is the most efficient way to do this?

Upvotes: 1

Views: 71

Answers (4)

Mike Müller
Mike Müller

Reputation: 85512

This works:

d = {'name': 'xyz','c1':['a','b','r','d','c'],'c2':['21','232','11','212','34']}
s = sorted(list(zip(d['c1'], d['c2'])))
d['c1'] = [x[0] for x in s]
d['c2'] = [x[1] for x in s]

Result:

{'c1': ['a', 'b', 'c', 'd', 'r'],
 'c2': ['21', '232', '34', '212', '11'],
 'name': 'xyz'}

UPDATE

The call to list is not needed. Thanks to tzaman for the hint. It was a relict of putting together the solution from separate steps.

d = {'name': 'xyz','c1':['a','b','r','d','c'],'c2':['21','232','11','212','34']}
s = sorted(zip(d['c1'], d['c2']))
d['c1'] = [x[0] for x in s]
d['c2'] = [x[1] for x in s]

Upvotes: 2

BlivetWidget
BlivetWidget

Reputation: 11073

Here's one way you could do it, using zip for both joining and unjoining. You can re-cast them as lists instead of tuples when you put them back into the dictionary.

a = ['a','b','r','d','c']
b = ['21','232','11','212','34']
mix = list(zip(a, b))
mix.sort()
a_new, b_new = zip(*mix)
>>> a_new
('a', 'b', 'c', 'd', 'r')
>>> b_new
('21', '232', '34', '212', '11')

Upvotes: 0

Prune
Prune

Reputation: 77860

Frankly, the most efficient way is to store the data in a form compatible with its use. In this case, you would use a table (e.g. PANDAS data frame) or simply a list:

xyz_table = [('a', '21'), ('b'. 232'), ...]

Then you simply sort the list on the first element of each tuple:

xyz_table = [('a', '21'), ('b', '232'), ('c', '34'), ('d', '212'), ('r', '11')]
xyz_sort = sorted(xyz_table, key = lambda row: row[0])

print xyz_sort

You could also look up a primer on Python sorting, such as this

Upvotes: 1

Muposat
Muposat

Reputation: 1506

Your data structure does not reflect the real relationship between its elements. I would start by merging c1 and c2 into an OrderedDict. It would take care of both the relationship and the order of elements. Like this:

dict = dict(
    name='xyz', 
    c = OrderedDict(sorted(
        zip(
            ['a','b','r','d','c'],
            ['21','232','11','212','34']
        )
    ))
)

Upvotes: 1

Related Questions