Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort a list of dicts according to a list of values with regex

I’d like to sort the keys of the list_of_dicts according to the list_months. It works fine once I remove the digits (years) from the keys of list_of_dicts, but I cannot figure out how to use the regex correctly in the lambda function to include the digits.

My code so far:

import re
list_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
list_of_dicts = [{'Apr23': '64.401'}, {'Aug23': '56.955'}, {'Dec23': '57.453'}, {'Feb23': '90.459'}, {'Jan23': '92.731'}, {'Jul23': '56.6'}, {'Jun23': '56.509'},{'Mar23': '86.209'}, {'May23': '58.705'}, {'Nov23': '57.368'}, {'Oct23': '56.711'}, {'Sep23': '57.952'}]

r = re.compile("[a-zA-Z]{3}[0-9]{2}")
    
print(sorted(list_of_dicts, key=lambda d: [k in d for k in list_months if re.search(r, k)], reverse=True))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

No need for a regex here.

dict_months = {m:i for i, m in enumerate(list_months)}
result = sorted(list_of_dicts, key=lambda d: dict_months[next(iter(d))[:3]])
print(result)
# [{'Jan23': '92.731'}, {'Feb23': '90.459'}, {'Mar23': '86.209'}, {'Apr23': '64.401'}, {'May23': '58.705'}, {'Jun23': '56.509'}, {'Jul23': '56.6'}, {'Aug23': '56.955'}, {'Sep23': '57.952'}, {'Oct23': '56.711'}, {'Nov23': '57.368'}, {'Dec23': '57.453'}]

If you also want to take the day into account, use

def sortby(d):
    key = next(iter(d))
    return dict_months[key[:3]], int(key[3:])

result = sorted(list_of_dicts, key=sortby)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading