-2

i have a list which have element as a student registration number and i have two dictionaries with the same student registration number and thier values are the student avg grades and the student surname and name i need a sorted list which sort the student registration number in descending order by average grade and, in case of a tie, in lexicographic order by the student's surname and name, finally registration number in ascending order for example

my_dict_1 = {'1882282': 29.4, '1675598': 29.125, '1659373': 29.25, '1324812': 30.4}   # this is dict with avg grades
my_dict_2 = {'1882282': 'Iacometti Monica', '1675598': "Fiala' Ester", '1659373': "Beudo' Miriam", '1324812': 'Abucar Osman Mariarosaria'}  # this is dict_2 with student surname and name

so my return sorted list should be

sorted_list = ['1324812', '1882282', '1659373', '1675598']
Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
Ejaz
  • 25
  • 1
  • 2
    `sorted` takes a `key` argument. This is a callable that takes the element being sorted and returns the "key" that you _actually_ want to sort it by. This "key" can be a tuple, which would result in sorting based on comparison of the tuple. – Pranav Hosangadi Sep 14 '22 at 13:19
  • 1
    Does this answer your question? [Sort a list by multiple attributes?](https://stackoverflow.com/questions/4233476/sort-a-list-by-multiple-attributes) This is not _quite_ an exact duplicate, but it shows how to use a key for multiple criteria, and should be good enough to understand what you need to do in your case. – Pranav Hosangadi Sep 14 '22 at 13:20
  • Does this include my all cases like I discussed in question that sort in decending with respect to avg grade if tie so lexicographical order of name and surname if tie so the student registration number it self in accending order – Ejaz Sep 14 '22 at 13:26

2 Answers2

0

do this :

import pandas as pd

new_dict = {'code': list(my_dict_1.keys()),
            'number': list(my_dict_1.values()),
            'name' : list(my_dict_2.values())}

df = pd.DataFrame(data=new_dict)

now we have a dataframe that has three columns : code , number and namber of them.

so it's time to sort it :

sorted_array = df.sort_values(['number', 'name', 'code'], ascending=False)

now convert anycolumn you want to the list:

sorted_list =  list(sorted_array['code']))

here you are:

['1324812', '1882282', '1659373', '1675598']
-3

this is just a try whe nu can sort by average then merge both dict so in case of a tie it will be ordered by the student's surname ascending :

my_dict_1 = {'1882282': 29.4, '1675598': 29.125, '1659373': 29.25, '1324812': 30.4}   # this is dict with avg grades
my_dict_2 = {'1882282': 'Iacometti Monica', '1675598': "Fiala' Ester", '1659373': "Beudo' Miriam", '1324812': 'Abucar Osman Mariarosaria'}
my_dict_1 = dict(sorted(my_dict_1.items(), key=lambda kv: kv[1]))
def defaultdict(default_type):
    class DefaultDict(dict):
        def __getitem__(self, key):
            if key not in self:
                dict.__setitem__(self, key, default_type())
            return dict.__getitem__(self, key)
    return DefaultDict()
dd  = defaultdict(list)

for d in (my_dict_1, my_dict_2): # you can list as many input dicts as you want here
    for key, value in d.items():
        dd[key].append(value)
print(dict(dd))
Wael Jlassi
  • 133
  • 1
  • 9