我有一本这样的字典,用来确定月份的顺序:
meses_ord = {'January':1, 'February': 2, 'March':3, ... }
我也有这样一个字典列表:
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')}]
我想按月份键排列字典表。你知道吗
我试过很多东西,但都没有成功:
fechas_ord = sorted(fechas_, key=operator.itemgetter(meses_ord[fechas_['mes']]))
假设您定义了如下变量
然后运行以下命令将返回一个排序的列表
你可以在Python Wiki和Python documentation找到更多信息
使用排序键函数查找月份:
排序函数也可以表示为lambda,只需确保它接受一个参数:
演示:
相关问题 更多 >
编程相关推荐