Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
Ordene una lista de dictados de acuerdo con una lista de valores con expresiones regulares

Me gustaría ordenar las claves de list_of_dicts según list_months . Funciona bien una vez que elimino los dígitos (años) de las claves de list_of_dicts , pero no puedo entender cómo usar la expresión regular correctamente en la función lambda para incluir los dígitos.

Mi código hasta ahora:

 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))
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

No hay necesidad de una expresión regular aquí.

 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'}]

Si también desea tener en cuenta el año, utilice

 def sortby(d): key = next(iter(d)) return int(key[3:]), dict_months[key[:3]] result = sorted(list_of_dicts, key=sortby)
over 4 years ago · Santiago Trujillo Report

0

Una forma de usar re.sub con list.index .

Tenga en cuenta que list.index es O(n) , que es bastante caro.

 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)

Producción:

 [{'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'}]
over 4 years ago · Santiago Trujillo Report

0

Realmente no necesita el dict_month , use las baterías de Python incluidas :

 from datetime import datetime sorted(list_of_dicts, key=lambda x: datetime.strptime(next(iter(x)), '%b%d'))

o:

 import dateutil.parser sorted(list_of_dicts, key=lambda x: dateutil.parser.parse(next(iter(x))))

producción:

 [{'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'}]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!