How to get the “next” item in an OrderedDict?

后端 未结 4 871
野的像风
野的像风 2021-02-08 01:44

I\'m using an OrderedDict to random access a list, but now want the next item in the list from the one that I have:

foo = OrderedDict([(\'apple\', 4         


        
4条回答
  •  无人共我
    2021-02-08 02:42

    Reworked from your code, this way I guess would be a little better:

    import collections as co
    import datetime as dt
    import itertools as it
    
    foo = co.OrderedDict([
        (dt.date(2000,1,1), 4),
        (dt.date(2000,5,23), 3),
        (dt.date(2000,10,1), 2),
        (dt.date(2000,12,31), 1)
    ])
    today = dt.date(2000,1,30)
    
    fooiter = it.dropwhile(lambda d: d <= today, foo)
    print next(fooiter)
    print list(fooiter)
    

    Basically having iterator at the right place is already enough.

    Would be cool to start iteration from any position, but not sure if-how possible. Needs some thought.

提交回复
热议问题