[英]Dictionary sort by numer of days in month( in keys) in python
I am trying to sort dictionary by number of days in month.我正在尝试按月的天数对字典进行排序。 (30,31 and 28).
(30,31 和 28)。 i have tried to achieve this by calendar module and datetime module but did not get the desired output.
我试图通过日历模块和日期时间模块来实现这一点,但没有得到所需的输出。 Any suggestion !!
任何建议!
product_sell= {'November':680,'August':678,'October':857,'January': 125,'April':989,'February': 300, 'September':120,'March':259,'December':899,'June':368, 'May':31,'July':968}
expected output (sort(keys) by number of months in days jan,march...=31, feb=28, april,june..=30 )预期输出(按天数排序(键) jan,march...=31, feb=28, april,june..=30 )
product_sell= {'January': 125, 'March':259, 'May':31, 'July':968, 'August':678, 'October':857, 'December':899, 'April':989, 'June':368, 'September':120, 'November':680, 'February': 300}
You can't sort dictionaries because it is orderless.您无法对字典进行排序,因为它是无序的。 You can use ordereddict to achieve this.
您可以使用ordereddict 来实现这一点。
from collections import OrderedDict
product_sell= {'November':680,'August':678,'October':857,'January': 125,'April':989,'February': 300, 'September':120,'March':259,'December':899,'June':368, 'May':31,'July':968}
months = ['January','February','March','April','May','June','July','August','September','October','November','December']
OrderedDict(sorted(product_sell.items(),key =lambda x:months.index(x[0])))
Output:输出:
OrderedDict([('January', 125),
('February', 300),
('March', 259),
('April', 989),
('May', 31),
('June', 368),
('July', 968),
('August', 678),
('September', 120),
('October', 857),
('November', 680),
('December', 899)])
You don't need to create a new (ordered) dictionary if you just want to consume (print, write to a file, ...) the data.如果您只想使用(打印、写入文件……)数据,则无需创建新的(有序)字典。 Just output a list of items
(month, count)
.只需输出项目列表
(month, count)
。
>>> product_sell= {'November':680,'August':678,'October':857,'January': 125,'April':989,'February': 300, 'September':120,'March':259,'December':899,'June':368, 'May':31,'July':968}
First, you can buld a sort key by month:首先,您可以按月构建排序键:
>>> import calendar
>>> key_by_month = dict(zip(calendar.month_name[1:], [(-calendar.monthrange(2019,i)[1], i) for i in range(1,13)]))
>>> key_by_month
{'January': (-31, 1), 'February': (-28, 2), 'March': (-31, 3), 'April': (-30, 4), 'May': (-31, 5), 'June': (-30, 6), 'July': (-31, 7), 'August': (-31, 8), 'September': (-30, 9), 'October': (-31, 10), 'November': (-30, 11), 'December': (-31, 12)}
Months will be sorted by number of days (descending, hence the minus sign) and then by their natural order.月份将按天数(降序,因此是减号)排序,然后按自然顺序排序。
Second, use sorted
and find the key by the month name:其次,使用
sorted
并按月份名称查找键:
>>> sorted(product_sell.items(), key=lambda m_c: key_by_month[m_c[0]])
[('January', 125), ('March', 259), ('May', 31), ('July', 968), ('August', 678), ('October', 857), ('December', 899), ('April', 989), ('June', 368), ('September', 120), ('November', 680), ('February', 300)]
Note: If you can, avoid using month names as keys, because they are subject to variations: "january" vs "January", foreign languages, ...注意:如果可以,请避免使用月份名称作为键,因为它们会发生变化:“一月”与“一月”、外语……
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.