I'm looking for a linked list implementation. Something iterable with
constant time insertion anywhere in the list. I was wondering if deque() is
the class to use or if there's something else. Is there?
Thank you...
Dec 9 '07
23 3869
Neil Cerutti <ho*****@yahoo. comwrote:
If you put an instrumented iterator through, say, reversed or
sorted, you'd lose the ability to use it to modify the list
I think that is kind of irrelevant. reversed doesn't take an iterator, it
requires a sequence and returns an iterator. sorted will take an iterator
but it always returns a new list.
I do have one last question about a doubly-linked list. Would you
have to perform any tricks (del statements) to get the garbage
collector to collect every node, or will it just work?
It should just work.
On 2007-12-11, Duncan Booth <du**********@i nvalid.invalidw rote:
Neil Cerutti <ho*****@yahoo. comwrote:
>If you put an instrumented iterator through, say, reversed or sorted, you'd lose the ability to use it to modify the list
I think that is kind of irrelevant. reversed doesn't take an
iterator, it requires a sequence and returns an iterator.
sorted will take an iterator but it always returns a new list.
Thank you! Strangely enough I didn't know either of those things.
I've been using sorted as if it were a generator, and I guess
I've never used reversed on an iterator before.
>I do have one last question about a doubly-linked list. Would you have to perform any tricks (del statements) to get the garbage collector to collect every node, or will it just work?
It should just work.
Cool.
--
Neil Cerutti
I am free of all prejudices. I hate everyone equally. --W. C. Fields
Neil Cerutti <ho*****@yahoo. comwrites:
Anyhow, implementing linked lists in Python is not challenging, but
they don't work well with Python iterators, which aren't suitable
for a linked list's purposes--so you have to give up the happy-joy
for loop syntax in favor of Python's frowny-sad while loops.
With the help of generators, it is trivial to turn a frowny loop into
a happy one:
class Node:
def __init__(self):
self.next = None
# attach data to the node as needed, and use "next" to refer
# to the next node.
def __iter__(self):
node = self
while 1:
yield node
node = node.next
if node is None:
break
def linkify(it):
"""Turn a Python iterable into a linked list."""
head = prev = None
for elem in it:
node = Node()
node.data = elem
if prev is None:
head = node
else:
prev.next = node
prev = node
return head
# iterate over a linked list using 'for':
>>linkedlist = linkify([1, 2, 3]) for n in linkedlist:
.... print n.data
....
1
2
3
Correctly handling empty lists is left as an excercise for the reader.
Duncan Booth <du**********@i nvalid.invalidw rites:
>I do have one last question about a doubly-linked list. Would you have to perform any tricks (del statements) to get the garbage collector to collect every node, or will it just work?
It should just work.
Fortunately that's trivial to verify:
class Node(object):
def __init__(self, prev):
self.prev = prev
if prev is not None:
prev.next = self
self.next = None
Now, simply create several nodes in a loop:
while 1:
head = Node(None)
node = Node(head)
node = Node(node)
If the Python process grows without bounds, the GC failed to do its
job.
GC in recent Python releases handles cyclic references flawlessly, at
least in the types shipped. For comparison, the equivalent Perl code
leaks like a hose. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: svilen |
last post by:
hello again.
i'm now into using python instead of another language(s) for
describing structures of data, including names, structure,
type-checks, conversions, value-validations, metadata etc. And i have
things to offer, and to request.
And a lot of ideas, but who needs them....
here's an example (from type_struct.py):
|
by: Ville Voipio |
last post by:
I would need to make some high-reliability software
running on Linux in an embedded system. Performance
(or lack of it) is not an issue, reliability is.
The piece of software is rather simple, probably a
few hundred lines of code in Python. There is a need
to interact with network using the socket module,
and then probably a need to do something hardware-
related which will get its own driver written in
C.
|
by: bruce |
last post by:
hi...
i'm trying to deal with multi-dimension lists/arrays
i'd like to define a multi-dimension string list, and then manipulate the
list as i need... primarily to add lists/information to the 'list/array' and
to compare the existing list information to new lists
i'm not sure if i need to import modules, or if the base python install i
have is sufficient. an example, or pointer to examples would be good...
|
by: efrat |
last post by:
Hello,
I'm planning to use Python in order to teach a DSA (data structures
and algorithms) course in an academic institute. If you could help out
with the following questions, I'd sure appreciate it:
1. What exactly is a Python list? If one writes a, then is the
complexity Theta(n)? If this is O(1), then why was the name "list"
chosen? If this is indeed Theta(n), then what alternative should be
used? (array does not seem suited for...
|
by: Kurt B. Kaiser |
last post by:
Patch / Bug Summary
___________________
Patches : 421 open ( +3) / 3530 closed ( +8) / 3951 total (+11)
Bugs : 963 open ( +4) / 6426 closed (+21) / 7389 total (+25)
RFE : 255 open ( +5) / 246 closed ( +1) / 501 total ( +6)
New / Reopened Patches
______________________
| |
by: Dongsheng Ruan |
last post by:
with a cell class like this:
#!/usr/bin/python
import sys
class Cell:
def __init__( self, data, next=None ):
self.data = data
|
by: Alex Snast |
last post by:
Hello
I'm new to python and i can't figure out how to write a reverse for
loop in python
e.g. the python equivalent to the c++ loop
for (i = 10; i >= 0; --i)
|
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,...
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
| |
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |