简体   繁体   English

python - 按顺序从字典中提取值

[英]python - extracting values from dictionary in order

I have a dictionary with some share related data: 我有一个包含一些共享相关数据的字典:

share_data = {'2016-06-13':{'open': 2190, 'close':2200}, '2015-09-10':{'open': 2870, 'close':2450} # and so on, circa 1,500 entries

is there a way of iterating over the dictionary in order, so the oldest date is retrieved first, then the one soon after etc? 有没有一种按顺序迭代字典的方法,所以最先检索最早的日期,然后很快检索一个?

thanks! 谢谢!

Sure, the default lexicographical order of your date strings will map to chronological order. 当然,日期字符串的默认词典顺序将按时间顺序映射。 So it is very easy: 这很容易:

for key in sorted(share_data.keys()):
    #do something

This post has some nice examples of custom sorting on dictionaries: http://pythoncentral.io/how-to-sort-python-dictionaries-by-key-or-value/ 这篇文章有一些很好的字典自定义排序示例: http//pythoncentral.io/how-to-sort-python-dictionaries-by-key-or-value/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM