2

可能这是重复的。我没有找到相同类型的问题 我有一个字典列表,例如

mylist=[
    {'month':'MAR2011','amount':90},
    {'month':'MAR2013','amount':190},
    {'month':'JUN2011','amount':290},
    {'month':'AUG2011','amount':930},
    {'month':'DEC2011','amount':30},
    {'month':'NOV2010','amount':40},
    {'month':'FEB 2013','amount':760},
    {'month':'SEP 2012','amount':330},
    {'month':'APR2013','amount':50},
    {'month':'NOV 2011','amount':450},
    {'month':'OCT 2012','amount':450},
]

我想得到这样的输出:

[
{'month':'FEB 2013','amount':760},
{'month':'MAR2011','amount':90},
{'month':'MAR2013','amount':190},
{'month':'APR 2013','amount':50},
{'month':'JUN2011','amount':290},
{'month':'AUG2011','amount':930},
{'month':'SEP 2012','amount':330},
{'month':'OCT 2012','amount':450},
{'month':'NOV2010','amount':40},
{'month':'NOV 2011','amount':450},
{'month':'DEC2011','amount':30},
]

不想考虑年份。按月份排序。

提前致谢

4

2 回答 2

8

创建将月份映射到序数的字典:

from calendar import month_abbr

month_to_index = {month.upper(): i for i, month in enumerate(month_abbr[1:])}

并用它来排序:

sorted(mylist, key=lambda d: month_to_index[d['month'][:3]])

演示:

>>> from calendar import month_abbr
>>> month_to_index = {month.upper(): i for i, month in enumerate(month_abbr[1:])}
>>> import pprint
>>> pprint.pprint(sorted(mylist, key=lambda d: month_to_index[d['month'][:3]]))
[{'amount': 760, 'month': 'FEB 2013'},
 {'amount': 90, 'month': 'MAR2011'},
 {'amount': 190, 'month': 'MAR2013'},
 {'amount': 50, 'month': 'APR2013'},
 {'amount': 290, 'month': 'JUN2011'},
 {'amount': 930, 'month': 'AUG2011'},
 {'amount': 330, 'month': 'SEP 2012'},
 {'amount': 450, 'month': 'OCT 2012'},
 {'amount': 40, 'month': 'NOV2010'},
 {'amount': 450, 'month': 'NOV 2011'},
 {'amount': 30, 'month': 'DEC2011'}]

要按月第一、第二年排序,请从键函数返回一个元组:

sorted(mylist, key=lambda d: (month_to_index[d['month'][:3]], d['month'][-4:]))

演示:

>>> pprint.pprint(sorted(mylist, key=lambda d: (month_to_index[d['month'][:3]], d['month'][-4:])))
[{'amount': 760, 'month': 'FEB 2013'},
 {'amount': 90, 'month': 'MAR2011'},
 {'amount': 190, 'month': 'MAR2013'},
 {'amount': 50, 'month': 'APR2013'},
 {'amount': 290, 'month': 'JUN2011'},
 {'amount': 930, 'month': 'AUG2011'},
 {'amount': 330, 'month': 'SEP 2012'},
 {'amount': 450, 'month': 'OCT 2012'},
 {'amount': 40, 'month': 'NOV2010'},
 {'amount': 450, 'month': 'NOV 2011'},
 {'amount': 30, 'month': 'DEC2011'}]

在这种特定情况下,没有区别,因为每月数据已经按排序顺序排列。

于 2013-07-07T18:06:00.907 回答
1

首先创建一个映射字典,将月份映射到数字:

>>> import re
>>> import pprint
>>> dic = {'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JULY':7,'AUG':8,'SEP':9,'OCT':10,'NOV':11,'DEC':12}
def func(x):
    m = re.search(r'[A-Z]+',x['month'])
    return dic[m.group(0)]
... 
>>> pprint.pprint(sorted(mylist, key = func))
[{'amount': 760, 'month': 'FEB 2013'},
 {'amount': 90, 'month': 'MAR2011'},
 {'amount': 190, 'month': 'MAR2013'},
 {'amount': 50, 'month': 'APR2013'},
 {'amount': 290, 'month': 'JUN2011'},
 {'amount': 930, 'month': 'AUG2011'},
 {'amount': 330, 'month': 'SEP 2012'},
 {'amount': 450, 'month': 'OCT 2012'},
 {'amount': 40, 'month': 'NOV2010'},
 {'amount': 450, 'month': 'NOV 2011'},
 {'amount': 30, 'month': 'DEC2011'}]
于 2013-07-07T18:07:06.397 回答