473,688 Members | 3,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3788
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******@lexic on.netwrote in message
news:11******** **************@ v45g2000cwv.goo glegroups.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...@lexic on.netwrote in message

news:11******** **************@ v45g2000cwv.goo glegroups.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__=='__ma in__':
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__=='__ma in__':
f(1<<18)

Feb 23 '07 #10

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

Similar topics

4
4703
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 more curiously, why it would be put in there as an unreliable function?) Rather than roll my own threaded fifo class, it would seem prudent to use Python's built-in Queue but the warning signs on a rather necessary function seem curious.
9
2777
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, you've >provided next to no useful (IMHO) information to >let anyone help you more than this This is about 5% of the code. Uses no locks.
1
2010
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 support Queue objects? A little background. I made a C dll that sets up a thread for collecting data from an NI data aquisition card. The thread builds a list and passes the list to to python via a callback, then python puts them in the Queue. ...
2
3459
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 confused. I've read stuff on this group and various website and book until my head is spinning... Here is a brief summary of what I'm trying to do and an example below.
2
1303
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... whats the best way to do this? I tried do loops and some attempts at threats for this... do loops uses process speed up cause of the constant execuation which i dont want... does anyone know of a way to instantaniously find out if a queue has an...
2
2564
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) <type 'collections.deque'> >>>
6
2861
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 example) or is doing some long calculations.
3
2034
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 the Queue.py code now to try to figure out how to make it work in 2.5, but as I am a beginner, I am having difficulty and would appreciate your help. Many thanks Jon
11
2624
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 into the Queue. WaitMsg(ids, msg) # this is the hard part WaitMsg will get only msg with certain ids, but this is not possible in Queue object, because Queue provides no method to peek into the
0
8528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8782
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6453
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4321
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4547
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2215
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.