how to implement a queue-like container with sort function

Chris Angelico rosuav at gmail.com
Thu Nov 28 21:03:00 EST 2013


On Fri, Nov 29, 2013 at 12:54 PM, iMath <redstone-cold at 163.com> wrote:
> the container is similar to queue ,but queue doesn't have a sort  function

It's either a queue that can be sorted, or a list with a length limit.
You could fairly easily implement either, because in Python, anything
can be subclassed. But I think possibly the easiest way is to simply
turn your queue into a list when you want to work with it:

>>> import queue
>>> a=queue.Queue(10)
>>> a.put("asdf")
>>> a.put("qwer")
>>> a.put("zxcv")
>>> a.put("1234")
>>> sorted(a.queue)
['1234', 'asdf', 'qwer', 'zxcv']

Though I don't know if this is what you meant. Putting too much onto a
queue.Queue will block. Were you wanting it, instead, to discard
entries? If so, which?

ChrisA



More information about the Python-list mailing list