简体   繁体   中英

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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