[英]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: sort函数也可以表示为lambda,只需确保它接受一个参数即可:
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 您可以在Python Wiki和Python文档中找到更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.