简体   繁体   中英

How to *lazily* iterate on reverse order of keys in OrderedDict?

I'd like to iterate on an OrderedDict in reverse order.

Ie reverse the order of:

for k, v in my_ordered_dict.iteritems():
   # < do stuff >

So far I've gotten a non-lazy version, by reversing a list:

for k, v in list(my_ordered_dict.iteritems())[::-1]:
   # < do stuff >

Any ideas how to make it better?

If you use reversed on the dict it should evaluate lazily:

for k, v in ((k, my_ordered_dict[k]) for k in reversed(my_ordered_dict)):
    .....

How does this work?

The key element here is a generator expression , which is evaluated lazily. So this will lazily evaluate over the keys of the ordered dict, and then return a tuple of the key and the dict value when needed.

I only need to support python 3:

In python 3, dict.items() is now a view and is evaluated lazily. Thus the above can be simplified to:

for k, v in reversed(my_ordered_dict.items()):

While Python 2.7 does have a viewitems() method, Python 2.7 OrderedDict views do not support the __reversed__ hook that reversed needs to reverse non-sequences.

You could for in range with the values and then subtract from the number of values.

items = my_ordered_dict.iteritems()
max_index = len(items)

for i in range(max_index):
    object = items[max_index - i - 1]

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