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))
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 year into account, use
def sortby(d):
key = next(iter(d))
return int(key[3:]), dict_months[key[:3]]
result = sorted(list_of_dicts, key=sortby)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With