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))
CodePudding user response:
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)
CodePudding user response:
One way using re.sub
with list.index
.
Note that list.index
is O(n)
, which is quite expensive.
def get_key_loc(dic):
k = list(dic.keys())[0]
return list_months.index(re.sub("\d ", "", k))
sorted(list_of_dicts, key=get_key_loc)
Output:
[{'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'}]
CodePudding user response:
You don't really need the dict_month
, use python's batteries included:
from datetime import datetime
sorted(list_of_dicts, key=lambda x: datetime.strptime(next(iter(x)), '%b%d'))
or:
import dateutil.parser
sorted(list_of_dicts, key=lambda x: dateutil.parser.parse(next(iter(x))))
output:
[{'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'}]