DanielStutzbach / blist

A list-like type with better asymptotic performance and similar performance on small lists
Other
310 stars 36 forks source link

Allow popping from end of sortedlist #17

Closed fmark closed 14 years ago

fmark commented 14 years ago

Currently you can only cal sortedlist.pop() to get the first element of a list. I would like to be able to pop from the end as well as the beginning of a list.

DanielStutzbach commented 14 years ago

Good idea. Will fix.

mikegraham commented 14 years ago

I implemented pop with an index argument and item/slice deletion in http://github.com/mikegraham/blist/commit/38a9ef757e02528d8da39d729ff80604be49a7e6

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import blist
>>> s = blist.sortedlist([1, 2, 3, 4, 5, 0, 9, 8, 7, 6])
>>> s
sortedlist([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s.pop()
0
>>> s
sortedlist([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s.pop(-1)
9
>>> s
sortedlist([1, 2, 3, 4, 5, 6, 7, 8])
>>> s.pop(2)
3
>>> s
sortedlist([1, 2, 4, 5, 6, 7, 8])
>>> del s[1]
>>> s
sortedlist([1, 4, 5, 6, 7, 8])
>>> del s[4:]
>>> s
sortedlist([1, 4, 5, 6])
>>> del s[:2]
>>> s
sortedlist([5, 6])
>>> s = blist.sortedlist([-4, -2, 0, 1, 3, 5], key=abs)
>>> s
sortedlist([0, 1, -2, 3, -4, 5])
>>> s.pop()
0
>>> s.pop(-2)
-4
>>> s
sortedlist([1, -2, 3, 5])
>>> del s[2:]
>>> s
sortedlist([1, -2])