473,394 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

What is the best queue implemetation in Python?

I want to write a code for Breadth First Traveral for Graph, which needs a
queue to implement.

I wonder that for such a powerful language as Python, whether there is a
better and simpler implementation for a traditional FIFO queue?
Thanks!
Feb 23 '07 #1
10 3774
On Feb 23, 11:12 am, "John" <rds1...@sh163.netwrote:
I want to write a code for Breadth First Traveral for Graph, which needs a
queue to implement.

I wonder that for such a powerful language as Python, whether there is a
better and simpler implementation for a traditional FIFO queue?
Better and simpler than *WHAT*?


Feb 23 '07 #2
Than C or PASCAL
I mean, list or dictionary in Python are so powerful than the traditional
array. Maybe I can make use of it?
"John Machin" <sj******@lexicon.netwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
On Feb 23, 11:12 am, "John" <rds1...@sh163.netwrote:
I want to write a code for Breadth First Traveral for Graph, which needs
a
queue to implement.

I wonder that for such a powerful language as Python, whether there is a
better and simpler implementation for a traditional FIFO queue?

Better and simpler than *WHAT*?


Feb 23 '07 #3
On Feb 23, 11:24 am, "John" <rds1...@sh163.netwrote:
Than C or PASCAL
I mean, list or dictionary in Python are so powerful than the traditional
array. Maybe I can make use of it?
Well, you could wite your own queue manager using Python lists,
but ...

You have this strange reluctance to look in the documentation. Have
you tried Google? Try http://docs.python.org/lib/deque-objects.html

Or perhaps you want/need the Queue module or the heapq module.

*You* find them and *you* work out what is best for *your* needs.

If you have a question that you could not answer yourself, then ask it
here.

HTH,
John


>
"John Machin" <sjmac...@lexicon.netwrote in message

news:11**********************@v45g2000cwv.googlegr oups.com...
On Feb 23, 11:12 am, "John" <rds1...@sh163.netwrote:
I want to write a code for Breadth First Traveral for Graph, which needs
a
queue to implement.
I wonder that for such a powerful language as Python, whether there is a
better and simpler implementation for a traditional FIFO queue?
Better and simpler than *WHAT*?

Feb 23 '07 #4
hg
John Machin wrote:
On Feb 23, 11:12 am, "John" <rds1...@sh163.netwrote:
>I want to write a code for Breadth First Traveral for Graph, which needs
a queue to implement.

I wonder that for such a powerful language as Python, whether there is a
better and simpler implementation for a traditional FIFO queue?

Better and simpler than *WHAT*?
Sorry, but you do that all the time ... "ask the question as you know the
answer, otherwise shut the f u ..."

Can't you assume for a second that other people do not have your wonderful
brain and still have to make it through 60+ years of life of learning ?

hg
Feb 23 '07 #5
hg
hg wrote:
f u
"f o" of course

Feb 23 '07 #6
John wrote:
I want to write a code for Breadth First Traveral for Graph, which needs a
queue to implement.

I wonder that for such a powerful language as Python, whether there is a
better and simpler implementation for a traditional FIFO queue?
For a BFS I coded up a while back iterating over a list of nodes (and
appending nodes to the list as dictated by the algorithm) did the job.

Duncan
Feb 23 '07 #7
John Machin wrote:
On Feb 23, 11:24 am, "John" <rds1...@sh163.netwrote:
>>Than C or PASCAL
I mean, list or dictionary in Python are so powerful than the traditional
array. Maybe I can make use of it?


Well, you could wite your own queue manager using Python lists,
but ...

You have this strange reluctance to look in the documentation. Have
you tried Google? Try http://docs.python.org/lib/deque-objects.html

Or perhaps you want/need the Queue module or the heapq module.

*You* find them and *you* work out what is best for *your* needs.

If you have a question that you could not answer yourself, then ask it
here.

HTH,
John
You could do yourself a favor and not answer. You would also be sparing
the rest of us your rude tone.

James
Feb 23 '07 #8
John:
I want to write a code for Breadth First Traveral for Graph, which needs a
queue to implement.
For that purpose I have used the good deque that you can find in
collections in the standard library. It's very good for queues, and
it's a bit faster than regular lists for stacks too.

Bye,
bearophile

Feb 23 '07 #9
For that purpose I have used the good deque that you can find in
collections in the standard library. It's very good for queues, and
it's a bit faster than regular lists for stacks too.
you mean *much* faster (since a list is a reference array so pop(0) is
O(n) operation)

never use a list as queue if len(queue) 10000

=== benchmark

$ time ./deque_queue.py
34359607296

real 0m0.286s
user 0m0.264s
sys 0m0.016s

$ time ./list_queue.py
34359607296

real 1m20.915s
user 1m18.649s
sys 0m0.396s
=== the sources

--- deque_queue.py:
#!/usr/bin/python2.5

from collections import deque

def f(n):
sum = 0
queue = deque()
for i in range(n):
queue.append(i)
while queue:
sum += queue.popleft()
print sum

if __name__=='__main__':
f(1<<18)

--- list_queue.py:
#!/usr/bin/python2.5

def f(n):
sum = 0
queue = list()
for i in range(n):
queue.append(i)
while queue:
sum += queue.pop(0)
print sum

if __name__=='__main__':
f(1<<18)

Feb 23 '07 #10
On Feb 22, 12:40 pm, hg <h...@nospam.orgwrote:
John Machin wrote:
On Feb 23, 11:12 am, "John" <rds1...@sh163.netwrote:
I want to write a code for Breadth First Traveral for Graph, which needs
a queue to implement.
I wonder that for such a powerful language as Python, whether there is a
better and simpler implementation for a traditional FIFO queue?
Better and simpler than *WHAT*?

Sorry, but you do that all the time ... "ask the question as you know the
answer, otherwise shut the f u ..."

Can't you assume for a second that other people do not have your wonderful
brain and still have to make it through 60+ years of life of learning ?

hg


Um... first off, you have to admit that a lot of what is posted
on the internet in general and even on groups like this is rambling,
poorly thought out, missing necessary context, and just generally a
mess and hard to understand.

So maybe a little peer pressure on folks to clean up their posts and
try
to express themselves clearly isn't such a bad thing.

And finally, let's face it: if the internet ever does cease to be a
place
where you can see crotechety know-it-all programmers roasting clueless
noobs,
then, honestly, what is the point of owning a computer?

Feb 23 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James R. Saker Jr. | last post by:
I see per pydoc that Queue.Queue()'s .qsize is allegedly unreliable: | qsize(self) | Return the approximate size of the queue (not reliable!). Any thoughts on why this is unreliable (and...
9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
1
by: scott.manton | last post by:
I'm a new user of the Python C interface. I would like to know if it is possible to put items on a standard python Queue object in C, and pop them from Python. Does the Python/C interface...
2
by: GregM | last post by:
Hi First off I'm not using anything from Twisted. I just liked the subject line :) The folks of this list have been most helpful before and I'm hoping that you'll take pity on a the dazed and...
2
by: Brian Henry | last post by:
I need to have an app that will have a queue, but sit dormant until something is placed in that queue, when it realizes the queue has items waiting start executeing the processes for thoes items......
2
by: SA Trygubenko | last post by:
Dear All, Python 2.4 (#1, Mar 22 2005, 21:42:42) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Queue >>> q=Queue.Queue() >>> type(q.queue)...
6
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for...
3
by: jrpfinch | last post by:
I have a script which is based on the following code. Unfortunately, it only works on Python 2.3 and not 2.5 because there is no esema or fsema attribute in the 2.5 Queue. I am hunting through...
11
by: Terry | last post by:
Hello! I'm trying to implement a message queue among threads using Queue. The message queue has two operations: PutMsg(id, msg) # this is simple, just combine the id and msg as one and put it...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.