根据第一项对元组列表进行排序
如何根据第一个值对元组列表进行排序,即在字典中我们可以使用 sorted(a.keys())
。
如何对元组列表执行此操作?
如果这些是元组值,
t = [('2010-09-11', 'somedata', somedata),
('2010-06-11', 'somedata', somedata),
('2010-09-12', 'somedata', somedata)]
则应根据第一个字段中的日期对元组进行排序。
How to sort a list of tuples based on the first value i.e, in a dictionary we can use sorted(a.keys())
.
How to do it for a list of tuples?
If these are the tuple values
t = [('2010-09-11', 'somedata', somedata),
('2010-06-11', 'somedata', somedata),
('2010-09-12', 'somedata', somedata)]
tuples should be sorted based on dates in the first field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,只需
sorted(t)
即可,因为元组按字典顺序排序。如果您确实想忽略第一项之后的所有内容(而不是按后面的元素对具有相同第一个元素的元组进行排序),您可以提供一个key
来挑选第一个元素。最简单的方法是operator.itemgetter
:当然,如果你想就地对列表进行排序,你可以使用
t.sort(key=operator.itemgetter(0))
代码> 代替。Usually, just
sorted(t)
works, as tuples are sorted by lexicographical order. If you really want to ignore everything after the first item (instead of sorting tuples with the same first element by the following elements), you can supply akey
that picks out the first element. The simplest way would beoperator.itemgetter
:Of course if you want to sort the list in-place, you can use
t.sort(key=operator.itemgetter(0))
instead.您可以使用非常简单的
查看: Python 如何对元组列表进行排序?
You can use the very simple
see: How does Python sort a list of tuples?
或者您可以使用类似的方法来确保元组列表按日期排序:
Or you can use something like this to be sure that list of tuples sorted by dates:
如果 '2010-09-11' 是 年-月-日 ,则执行:
result
。
如果“2010-09-11”是年-日-月,则执行:
result
。
编辑1
阅读Artsiom Rudzenka的答案,他单独使用strptime(),我意识到strptime()产生一个struct_time对象按性质排序。这样的对象具有属性
tm_year、tm_mon、tm_mday、tm_hour、tm_min、tm_sec、tm_wday、tm_yday、tm_isdst
,这些属性可通过常见的点表示法访问(toto.tm_mon for示例),也可以通过索引表示法访问(例如toto[1]),因为struc_time对象的属性是按照tm_year、tm_mon、tm_mday、tm_hour、tm_min、tm_sec的顺序注册的, tm_wday、tm_yday、tm_isdst 。 struct_time 数据类型有一个命名元组的接口。由于 struct_time 对象是按性质排序的,因此无需应用 strftime() 来获取具有
年-月-日 按此顺序:此顺序已存在于 struct_time 对象中。
然后,我更正了“2010-06-11”中的 11 是 月份 的情况的代码:我消除了 strftime()
编辑 2
考虑 Kirk Strauser 的信息:
If '2010-09-11' is year-month-day , you do:
result
.
If '2010-09-11' is year-day-month, you do:
result
.
Edit 1
Reading the answer of Artsiom Rudzenka in which he uses strptime() alone, I realized that strptime() produces a struct_time object that is sorted by nature . Such an object has attributes
tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst
that are accessible through common dot-notation access (toto.tm_mon for exemple), but also through index-notation access (toto[1] for exemple) , because the attributes of a struc_time object are registered in this ordertm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst
. The struct_time data type has a named tuple's interface .Since a struct_time object is ordered by nature, it isn't necessary to apply strftime() to obtain a date string having
year-month-day
in this order: this order is already present in the struct_time object.Then , I correct my code for the case in which 11 in '2010-06-11' is the month : I eliminate strftime()
Edit 2
Taking Kirk Strauser's info in consideration: