Home > Software engineering >  Python sort a list of dicts using a specified mapping
Python sort a list of dicts using a specified mapping

Time:09-22

So, I have a list of dicts in python that looks like this:

lis =
[
{'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'},
{'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
...
]

Now I want lis to be in a way, such that each individual dict is ordered using the mapping for the keys that I will provide. For example, if

mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}

Then, I want to make my original list of dicts look like this:

lis =
[
{'date': '2021-05-07 01:59:37', 'Genre': 10, 'action': 'Notify', 'type': 'Something'},
{'date': '2021-05-07 01:59:37', 'Genre': 20, 'action': 'Notify', 'type': 'Something Else'}
...
]

How do I implement this?

CodePudding user response:

You might harness collections.OrderedDict for this task as follows

import collections
order = ['date', 'Genre', 'action', 'type']
dct1 = {'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'}
dct2 = {'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
odct1 = collections.OrderedDict.fromkeys(order)
odct1.update(dct1)
odct2 = collections.OrderedDict.fromkeys(order)
odct2.update(dct2)
print(odct1)
print(odct2)

output:

OrderedDict([('date', '2021-05-07 01:59:37'), ('Genre', 10), ('action', 'Notify'), ('type', 'Something')])
OrderedDict([('date', '2021-05-07 01:59:37'), ('Genre', 20), ('action', 'Notify'), ('type', 'Something Else')])

Disclaimer: this assume every dict you want to process has exactly all keys from order. This solution works with any python version which has collections.OrderedDict if you will be using solely python3.7 or newer you might use common dict as follows

order = ['date', 'Genre', 'action', 'type']
dct1 = dict.fromkeys(order)
dct1.update({'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'})
print(dct1)

output

{'date': '2021-05-07 01:59:37', 'Genre': 10, 'action': 'Notify', 'type': 'Something'}

Disclaimer still holds

CodePudding user response:

With a list comprehension:

lis = [
{'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'},
{'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
]

mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}

sorted_lis = [
    {field: record[field] for field in mapping.values()}
    for record in lis
]

print(sorted_lis)

CodePudding user response:

Try this:

def sort_dct(li, mapping):
    return {v: li[v] for k,v in mapping.items()}

out = []
mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}
for li in lis:
    out.append(sort_dct(li,mapping))
    
print(out)

Output:

[{'date': '2021-05-07 01:59:37',
  'Genre': 10,
  'action': 'Notify',
  'type': 'Something'},
 {'date': '2021-05-07 01:59:37',
  'Genre': 20,
  'action': 'Notify',
  'type': 'Something Else'}]
  • Related