Home > OS >  sort a defaultdict(list) by highest item number
sort a defaultdict(list) by highest item number

Time:12-21

I have the following code that I have created from running some analysis and I have put the results in a defaultdict(list). Afterwards I put the results into a csv file. Id like to sort the values that Im returning so that the more important items go to the top.

I want to sort my defaultdict(list) by "Check1" from greatest to least.

How would I sort the values so that for each key(address) the dictionary with the highest "Check1" comes first?

defaultdict(list,
                     {'Address_1': [{'Name': 'name',
               'Address_match': 'address_match_1',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 8,
                 'Check2' : 1},
              {'Name': 'name',
               'Address_match': 'address_match_2',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 20,
                 'Check2' : 1},
              {'Name': 'name',
               'Address_match': 'address_match_3',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 27,
                 'Check2' : 1}],
              'Address_2': [{'Name': 'name',
               'Address_match': 'address_match_1',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 30,
                 'Check2' : 1},
              {'Name': 'name',
               'Address_match': 'address_match_2',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 38,
                 'Check2' : 1},
              {'Name': 'name',
               'Address_match': 'address_match_3',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 12,
                 'Check2' : 1}]})

Result would look like this:

defaultdict(list,          {'Address_1': [{'Name': 'name',
               'Address_match': 'address_match_1',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 27,
                 'Check2' : 1},
              {'Name': 'name',
               'Address_match': 'address_match_2',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 20,
                 'Check2' : 1},
              {'Name': 'name',
               'Address_match': 'address_match_3',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 8,
                 'Check2' : 1}],
              'Address_2': [{'Name': 'name',
               'Address_match': 'address_match_1',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 38,
                 'Check2' : 1},
              {'Name': 'name',
               'Address_match': 'address_match_2',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 30,
                 'Check2' : 1},
              {'Name': 'name',
               'Address_match': 'address_match_3',
               'ID': 'id',
               'Type': 'abc',
                'Check1' : 12,
                 'Check2' : 1}]})

CodePudding user response:

You can get your expected outcome by using sorted function dict comprehension

out = {k: sorted(lst, key=lambda x: -x.get('Check1')) for k, lst in your_defaultdict.items()}

Output:

{'Address_1': [{'Name': 'name',
   'Address_match': 'address_match_3',
   'ID': 'id',
   'Type': 'abc',
   'Check1': 27,
   'Check2': 1},
  {'Name': 'name',
   'Address_match': 'address_match_2',
   'ID': 'id',
   'Type': 'abc',
   'Check1': 20,
   'Check2': 1},
  {'Name': 'name',
   'Address_match': 'address_match_1',
   'ID': 'id',
   'Type': 'abc',
   'Check1': 8,
   'Check2': 1}],
 'Address_2': [{'Name': 'name',
   'Address_match': 'address_match_2',
   'ID': 'id',
   'Type': 'abc',
   'Check1': 38,
   'Check2': 1},
  {'Name': 'name',
   'Address_match': 'address_match_1',
   'ID': 'id',
   'Type': 'abc',
   'Check1': 30,
   'Check2': 1},
  {'Name': 'name',
   'Address_match': 'address_match_3',
   'ID': 'id',
   'Type': 'abc',
   'Check1': 12,
   'Check2': 1}]}

CodePudding user response:

Not sure what's the problem is, pretty straightforward:

for key, values in dictionary.items():
    values.sort(key=lambda value: value['Check1'], reverse=True)
  • Related