Raymond Hettinger added the comment:

Please don't rush to make patches.  It isn't even clear that this is a good 
idea.

AFAICT, none of the many extant implementation of ordered dictionaries in any 
language currently implement a rotate operation.

FWIW, the iter() and move_to_end() methods are likely your best bet for 
implementing a rotate function using the current API and without doing any 
ordered dictionary key lookups:

>>> from collections import OrderedDict
>>> from itertools import islice
>>> 
>>> def rotate(d, n):
    # quick demo
    if n > 0:
        for k in list(islice(d, n)):
            d.move_to_end(k)
    elif n < 0:
        for k in list(islice(reversed(d), -n)):
            d.move_to_end(k, 0)
           
>>> d = collections.OrderedDict.fromkeys('abcdefghijk')
>>> list(d)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
>>> rotate(d, 3)
>>> list(d)
['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'a', 'b', 'c']
>>> rotate(d, -3)
>>> list(d)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue17100>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to