79231491

Date: 2024-11-27 18:38:38
Score: 1
Natty:
Report link

import pandas as pd

month_list = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] months = { 'Jan': 0, 'Feb': 1, 'Mar': 2, 'Apr': 3, 'May': 4, 'Jun': 5, 'Jul': 6, 'Aug': 7, 'Sep': 8, 'Oct': 9, 'Nov': 10, 'Dec': 11, } data = [['Sep', '2024', 112], ['Dec', '2022', 79], ['Apr', '2023', 114], ['Aug', '2024', 194], ['May', '2022', 140], ['Jan', '2023', 222]]

sorted_data = sorted(data, key=lambda x: (int(x[1]), months[x[0]]))

print(sorted_data)

The code sorts the data list first by year (ascending) and then by the month's order using the months dictionary.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: user28518730