[英]Find max and min date
I have a list with with dates. 我有一个日期列表。 Currently I am interested only in the key 1 and 2 of each tuple.
目前我只对每个元组的关键1和2感兴趣。
month/year
search_count=[(1L, 4, 2016), (3L, 5, 2016)]
I am trying this code, but in this way I only get the max and min year. 我正在尝试这个代码,但是这样我才能获得最大和最小年份。 I am interested in the combination of month and year.
我对月份和年份的组合感兴趣。
min_date = min(search_count,key=itemgetter(2))
max_date = max(search_count,key=itemgetter(2))
How can I do that? 我怎样才能做到这一点? Seem like i need two keys in min and max.
好像我需要最小和最大两个键。
You can pass multiple indices into itemgetter
. 您可以将多个索引
itemgetter
。 First order by 2
(year), then order by 1
(month): 首先按
2
(年)排序,然后按1
(月)排序:
min_date = min(search_count, key=itemgetter(2, 1))
max_date = max(search_count, key=itemgetter(2, 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.