0

I have a dictionary

my_dict = {'name':['a','b','c','d','e'],'c1':[0,0,1,2,0],'c2':[3,0,2,1,2],'c3':[1,2,3,4,5]}

I want to sort the dictionary based on the value of c1 and c2. How can I do that? I.e. Priority to sort the dict is using C1 but if C1 has to same values like c1[0] and c1[1] at that time it short the data based on C2 data.

I have tried below but getting error:

c1_list = count_dict.get('c1')
c1_list.sort(reverse=True)

index_map = {v: i for i, v in enumerate(c1_list)}
print sorted(my_dict.items(), key=lambda pair: index_map[pair[0]])

Expected Output:

{'name':['d','c','a','e','b'],'c1':[2,1,0,0,0],'c2':[1,2,3,2,0],'c3':[4,3,1,5,2]}
  • This is not giving expected results. I have to sort the dictionary based on the value of a list. not the key – Jaimin Mody Jan 25 '19 at 09:39
  • Can someone explain to me how values of `name` and `c3` are sorted in order like that if it's based on both `c1` and `c2`? – benji Jan 25 '19 at 10:14
  • What do you mean by "sort by value of `c1` __and__ `c2`? What does the __and__ mean in that context? The sum of both values? And you do not want to sort the dictionary, you apparently want to sort the values of the keys according to the unknown rule. – Jan Christoph Terasa Jan 25 '19 at 10:38
  • @JanChristophTerasa C1 and C2 means first we need to sort using C1 but if C1 has 2 same values then in that case for those similar values it should use C2 for sorting – Jaimin Mody Jan 25 '19 at 10:40
  • Your toy example works if I take the sum of the values of `c1` and `c2`. I do not understand the rule "if C1 has 2 same values" if you want to sort your values elementwise. – Jan Christoph Terasa Jan 25 '19 at 10:42
  • Check [my answer](https://stackoverflow.com/questions/54362407/dictionary-sorting-based-on-a-list-in-the-value/54363906#54363906) for sorting based on the order `c1, c2`. – Jan Christoph Terasa Jan 25 '19 at 11:02

2 Answers2

1

If you want to sort all values in specific reversed order, here values of c2 and c1, you could zip() them together and use key parameter of sorted() to sort all values in saved order.

Python 3

my_dict = my_dict = {'name':['a','b','c','d','e'],'c1':[0,0,1,2,0],'c2':[3,0,2,1,2],'c3':[1,2,3,4,5]}
sort_by = [my_dict['c1'], my_dict['c2']]

for k, v in my_dict.items():
    my_dict[k] = [i for *_, i in sorted(zip(*sort_by, v), key=lambda x: x[:-1], reverse=True)]

print(my_dict)

Output:

{'name': ['d', 'c', 'a', 'e', 'b'], 'c1': [2, 1, 0, 0, 0], 'c2': [1, 2, 3, 2, 0], 'c3': [4, 3, 1, 5, 2]}

Python 2

my_dict = my_dict = {'name':['a','b','c','d','e'],'c1':[0,0,1,2,0],'c2':[3,0,2,1,2],'c3':[1,2,3,4,5]}
sort_by = [my_dict['c1'], my_dict['c2']]

for k, v in my_dict.items():
    my_dict[k] = [i[0] for i in sorted(zip(v, *sort_by), key=lambda x: x[1:], reverse=True)]

print(my_dict)

Output:

{'name': ['d', 'c', 'a', 'e', 'b'], 'c1': [2, 1, 0, 0, 0], 'c2': [1, 2, 3, 2, 0], 'c3': [4, 3, 1, 5, 2]}
Filip Młynarski
  • 3,534
  • 1
  • 10
  • 22
0

Pandas

Normally I'd use numpy for this, but since it does not have a convenient constructor for conversion of dictionaries to structured arrays, I recommend to use pandas:

import pandas as pd

my_dict = {'name':['a','b','c','d','e'],'c1':[0,0,1,2,0],'c2':[3,0,2,1,2],'c3':[1,2,3,4,5]}

df = pd.DataFrame(my_dict)
df_sorted = df.sort_values(by=['c1', 'c2'])[::-1]
sorted_dict = df_sorted.to_dict('list')

Output:

 {'name': ['d', 'c', 'a', 'e', 'b'], 'c1': [2, 1, 0, 0, 0], 'c2': [1, 2, 3, 2, 0], 'c3': [4, 3, 1, 5, 2]}
Jan Christoph Terasa
  • 5,781
  • 24
  • 34