简体   繁体   中英

Order a list of dictionaries based on another dictionary

I have a dictionary like this, to stablish the order of the months:

meses_ord = {'January':1, 'February': 2, 'March':3, ... }

And I also have a list of dictionaries like this:

fechas_ = [{'anyo': 2010, 'horas': Decimal('52.5'), 'month': 'March', 'importe': Decimal('4200.000')},
{'anyo': 2010, 'horas': Decimal('40.0'), 'month': 'February', 'importe':Decimal('3200.000')},
{'anyo': 2010, 'horas': Decimal('42.5'), 'month': 'April', 'importe': Decimal('3400.000')},
{'anyo': 2010, 'horas': Decimal('20.0'), 'month': 'January', 'importe': Decimal('1600.000')}]

I want to order the list of dictionaries based on the month key.

I have tried many things, but none has worked:

fechas_ord = sorted(fechas_, key=operator.itemgetter(meses_ord[fechas_['mes']]))

Use a sort key function to look up the month:

def sort_by_month(entry):
    return meses_ord[entry['month']]

sorted(fechas_, key=sort_by_month)

The sort function can be expressed as a lambda too, just make sure it takes an argument:

sorted(fechas_, key=lambda entry: meses_ord[entry['month']])

Demo:

>>> from decimal import Decimal
>>> from pprint import pprint
>>> meses_ord = {'January': 1, 'February': 2, 'March': 3, 'April': 4}
>>> fechas_ = [{'anyo': 2010, 'horas': Decimal('52.5'), 'month': 'March', 'importe': Decimal('4200.000')},
... {'anyo': 2010, 'horas': Decimal('40.0'), 'month': 'February', 'importe':Decimal('3200.000')},
... {'anyo': 2010, 'horas': Decimal('42.5'), 'month': 'April', 'importe': Decimal('3400.000')},
... {'anyo': 2010, 'horas': Decimal('20.0'), 'month': 'January', 'importe': Decimal('1600.000')}]
>>> pprint(sorted(fechas_, key=lambda entry: meses_ord[entry['month']]))
[{'anyo': 2010,
  'horas': Decimal('20.0'),
  'importe': Decimal('1600.000'),
  'month': 'January'},
 {'anyo': 2010,
  'horas': Decimal('40.0'),
  'importe': Decimal('3200.000'),
  'month': 'February'},
 {'anyo': 2010,
  'horas': Decimal('52.5'),
  'importe': Decimal('4200.000'),
  'month': 'March'},
 {'anyo': 2010,
  'horas': Decimal('42.5'),
  'importe': Decimal('3400.000'),
  'month': 'April'}]

Assuming you have defined your variables as follows

months = {'January':1, 'February': 2, 'March':3, 'April':4 }
stuff = [{'anyo': 2010, 'horas': Decimal('52.5'), 'month': 'March', 'importe': Decimal('4200.000')},
{'anyo': 2010, 'horas': Decimal('40.0'), 'month': 'February', 'importe':Decimal('3200.000')},
{'anyo': 2010, 'horas': Decimal('42.5'), 'month': 'April', 'importe': Decimal('3400.000')},
{'anyo': 2010, 'horas': Decimal('20.0'), 'month': 'January', 'importe': Decimal('1600.000')}]

Then running the following will return a sorted list

sorted(stuff, key=lambda stuffa: months[stuffa['month']])

You can find out more here at the Python Wiki and Python documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM