Message181184
| Author |
rhettinger |
| Recipients |
Ramchandra Apte, eric.snow, pitrou, rhettinger, serhiy.storchaka |
| Date |
2013年02月02日.17:38:44 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1359826724.85.0.257742832191.issue17100@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
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'] |
|