根据第一项对元组列表进行排序

发布于 2024-11-26 03:10:04 字数 292 浏览 1 评论 0原文

如何根据第一个值对元组列表进行排序,即在字典中我们可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世晨晓 2024-12-03 03:10:04

通常,只需 sorted(t) 即可,因为元组按字典顺序排序。如果您确实想忽略第一项之后的所有内容(而不是按后面的元素对具有相同第一个元素的元组进行排序),您可以提供一个key来挑选第一个元素。最简单的方法是operator.itemgetter

import operator
...
for item in sorted(t, key=operator.itemgetter(0)):
    ...

当然,如果你想就地对列表进行排序,你可以使用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 a key that picks out the first element. The simplest way would be operator.itemgetter:

import operator
...
for item in sorted(t, key=operator.itemgetter(0)):
    ...

Of course if you want to sort the list in-place, you can use t.sort(key=operator.itemgetter(0)) instead.

耀眼的星火 2024-12-03 03:10:04

您可以使用非常简单的

t.sort()

查看: Python 如何对元组列表进行排序?

You can use the very simple

t.sort()

see: How does Python sort a list of tuples?

疑心病 2024-12-03 03:10:04

或者您可以使用类似的方法来确保元组列表按日期排序:

from datetime import datetime
initData = [('2010-09-11','somedata',1), ('2010-06-11','somedata',2), ('2010-09-12','somedata',3)]
outData = sorted(initData , key=lambda x: datetime.strptime(x[0],"%Y-%m-%d"))

Or you can use something like this to be sure that list of tuples sorted by dates:

from datetime import datetime
initData = [('2010-09-11','somedata',1), ('2010-06-11','somedata',2), ('2010-09-12','somedata',3)]
outData = sorted(initData , key=lambda x: datetime.strptime(x[0],"%Y-%m-%d"))
夜唯美灬不弃 2024-12-03 03:10:04

如果 '2010-09-11' 是 年--日 ,则执行:

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

from operator import itemgetter
t.sort(key = itemgetter(0))
print t

result

[('2010-06-11', 'somedata', 'jyhghg'),
 ('2010-08-12', 'somedata', 'jyhghg'),
 ('2010-09-11', 'somedata', 'jyhghg'),
 ('2010-09-12', 'somedata', 'jyhghg')]

如果“2010-09-11”是年-日-,则执行:

from time import strptime,strftime

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: strftime('%Y%m%d',strptime(x[0],'%Y-%d-%m')))
print t

result

[('2010-06-11', 'somedata', 'jyhghg'),
 ('2010-09-11', 'somedata', 'jyhghg'),
 ('2010-08-12', 'somedata', 'jyhghg'),
 ('2010-09-12', 'somedata', 'jyhghg')]

编辑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()

from time import strptime

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: strptime(x[0],'%Y-%d-%m'))
print t

编辑 2

考虑 Kirk Strauser 的信息:

import re

regx = re.compile('(\d{4})-(\d\d)-(\d\d)')

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: regx.match(x[0]).group(1,3,2))
print t

If '2010-09-11' is year-month-day , you do:

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

from operator import itemgetter
t.sort(key = itemgetter(0))
print t

result

[('2010-06-11', 'somedata', 'jyhghg'),
 ('2010-08-12', 'somedata', 'jyhghg'),
 ('2010-09-11', 'somedata', 'jyhghg'),
 ('2010-09-12', 'somedata', 'jyhghg')]

.

If '2010-09-11' is year-day-month, you do:

from time import strptime,strftime

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: strftime('%Y%m%d',strptime(x[0],'%Y-%d-%m')))
print t

result

[('2010-06-11', 'somedata', 'jyhghg'),
 ('2010-09-11', 'somedata', 'jyhghg'),
 ('2010-08-12', 'somedata', 'jyhghg'),
 ('2010-09-12', 'somedata', 'jyhghg')]

.

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 order tm_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()

from time import strptime

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: strptime(x[0],'%Y-%d-%m'))
print t

Edit 2

Taking Kirk Strauser's info in consideration:

import re

regx = re.compile('(\d{4})-(\d\d)-(\d\d)')

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: regx.match(x[0]).group(1,3,2))
print t
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文