[英]Sort python dictionary by Keys formatted as strings
I have a dictionary with strings as keys formatted as yyyy-mm-dd and want to sort the dictionary by keys with the earliest dates first: 我有一个字符串字符串作为格式为yyyy-mm-dd的键,并希望按最早的日期键排序字典:
I am currently using sorted(datesAndText.keys())
but this isn't reliably working because the month and day fields are not always zero padded. 我目前正在使用
sorted(datesAndText.keys())
但这并不可靠,因为月和日字段并不总是填零。
I have looked at Sort python dictionary by date keys and How do I sort this list in Python, if my date is in a String? 我按日期键查看了排序python字典 ,如果我的日期是字符串 , 如何在Python中对此列表进行排序? but I can't seem to adopt them to by specific case.
但我似乎无法通过具体案例采纳它们。
Are you sure your keys are exactly in the format yyyy-mm-dd
? 你确定你的键完全符合
yyyy-mm-dd
的格式吗? For example: 例如:
>>> '2010-1-15' < '2010-02-15'
False
You may be forced to sort something like this: 您可能被迫对此进行排序:
sorted(d,key=lambda x: [int(y) for y in x.split('-')])
Another solution (assuming your years are all 4 digits): 另一个解决方案(假设您的年份都是4位数):
sorted(d,key=lambda x: [y.zfill(2) for y in x.split('-')])
I'm not sure which would be faster. 我不确定哪个更快。 I suppose it's a candidate for
timeit
. 我想这是一个候选
timeit
。
Dates in yyyy-mm-dd
format sort the same way both alphabetically and chronologically, so you can use standard sorted
: yyyy-mm-dd
格式的日期按字母顺序和时间顺序sorted
,因此您可以使用标准sorted
:
for k, v in sorted(datesAndText.items()):
# do something with key and value
Your format, yyyy-mm-dd
, allows a lexicographic sort, so your code should work fine unless your values aren't zero padded (ex 2012-10-9
instead of 2012-10-09
). 您的格式
yyyy-mm-dd
允许使用字典排序,因此您的代码应该正常工作,除非您的值不是零填充( 2012-10-9
而不是2012-10-09
)。
Fix this problem by relying on a comparison of dates rather than strings: 通过依赖日期而不是字符串的比较来解决此问题:
sorted(datesAndText, key=lambda x: datetime.strptime(x, '%Y-%m-%d'))
This utilizes the key
parameter to sorted, which is a function which accepts one argument (an element of the list being compared during sort) and returns a value on which sorted
can use to sort. 这利用了
key
参数来排序,这是它接受一个参数(排序期间被比较的列表的元素),返回在其上的值的函数sorted
可以用它来进行排序。
This has the ancillary benefit of allowing you to explicitly specify the string format of the date, should your data need to change. 如果您的数据需要更改,这具有允许您明确指定日期的字符串格式的辅助优势。
Edit: 编辑:
mgilson brought up an interesting point. mgilson提出了一个有趣的观点。
str.split
is probably more efficient. str.split
可能更有效率。 Let's see if he's correct: 让我们看看他是否正确:
strptime
solution: strptime
解决方案:
bburns@virgil:~$ python -mtimeit -s"from datetime import datetime;d={'2012-2-12':None, '2012-10-9':None, '1978-1-1':None, '1985-10-9':None}" 'sorted(d, key=lambda x: datetime.strptime(x,"%Y-%m-%d"))'
10000 loops, best of 3: 79.7 usec per loop
mgilson's original str.split
solution: mgilson最初的
str.split
解决方案:
bburns@virgil:~$ python -mtimeit -s"from datetime import datetime;d={'2012-2-12':None, '2012-10-9':None, '1978-1-1':None, '1985-10-9':None}" 'sorted(d,key=lambda x: [int(y) for y in x.split("-")])'
100000 loops, best of 3: 17.6 usec per loop
mgilson's zfill
str.split
solution: mgilson的
zfill
str.split
解决方案:
bburns@virgil:~$ python -mtimeit -s"from datetime import datetime;d={'2012-2-12':None, '2012-10-9':None, '1978-1-1':None, '1985-10-9':None}" 'sorted(d,key=lambda x: [y.zfill(2) for y in x.split("-")])'
100000 loops, best of 3: 7.4 usec per loop
Looks like he's correct! 看起来他是对的! mgilson's original answer is 4-5 times faster, and his final answer is 10-11 times faster!
mgilson的原始答案速度提高了4-5倍,他的最终答案速度提高了10-11倍! However, as we agreed in the comments, readability matters.
但是,正如我们在评论中所同意的那样,可读性至关重要。 Unless you're presently CPU-bound, I'd still advise going with
datetime.strptime
over str.split
. 除非你目前受CPU限制,否则我仍然建议在
str.split
使用datetime.strptime
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.