I am wanting to iterate through my linked list and only retrieve the value at a specific index. I have written a very slow way of accomplishing this:
`
def get_index(self, data):
if self.head is None:
raise Exception("List is empty")
reg_list = []
for i in self:
reg_list.append(i)
print(reg_list[data])
`
This code works but I realize this is not the fastest nor the best method for completing this operation. How can I write this better and without creating a standard list to accomplish the desired outcome?
For clarity as some are asking, here is the entirety of relevant code to make this run as I have written it and to show it is indeed a linked list:
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return self.data
class Linked_List:
def __init__(self, nodes=None):
self.head = None
if nodes is not None:
node = Node(data=nodes.pop(0))
self.head = node
for elem in nodes:
node.next = Node(data=elem)
node = node.next
def __repr__(self):
node = self.head
nodes = []
while node is not None:
nodes.append(node.data)
node = node.next
nodes.append("None")
return " -> ".join(nodes)
def __iter__(self):
node = self.head
while node is not None:
yield node
node = node.next
def get_index(self, data):
if self.head is None:
raise Exception("List is empty")
reg_list = []
for i in self:
reg_list.append(i)
print(reg_list[data])
```
It is confusing that your code names the argument data while it is supposed to be an index, and then names the loop variable i, which is supposed to be a node. Why not choose names that actually say what it is? Like index and node?
You can change the given code to the following:
def get_index(self, index):
return next((node for i, node in enumerate(self) if i == index), None)
Note that now:
The function returns the node. This is appropriate for such a method. Only printing the result would make the method less useful.
No error is raised when the list is empty. There is no reason to treat an empty list differently. The function will just return None when the given index is out of range, whether the list is empty or not.
I am learning Python and have developed few web applications etc. Now, I want to dig deeper and learn about the under the hood workings of Python. For that, I would like to make my own list iterable. Here is my effort so far:
class CustomList:
def __init__(self,*args):
self.nums=args
self.count=0
i=0
for arg in args:
i+=1
self.total=i
def __iter__(self):
return self
def __next__(self):
if self.count >= self.total:
raise StopIteration
self.count+=1
mylist=CustomList(1,2,3,4)
for item in mylist:
print(item)
Now, in my next function, I am unsure how to iterate through my self.nums so that my print(item) prints each item in the self.nums one by one.
I don't really want to use anything related to len(), append() etc. I want to create them on my own. So that's the future plan. For now, I can't even iterate through the user given *args.
You need to go back another level. args in your MyList(*args) is already an iterable.
Each list item needs to explicitly point to the next one. So each list item needs a record of the next pointer and the data associated with it. This could be a dict but then MyList.append would need to explicitly access the records. For me the MyListItem class is clearer.
class MyListItem:
def __init__(self, data):
self.next = None
self.data = data
def link_to(self, child):
self.next = child
The MyList class can then use this as the nodes in it's list structure. There may be better implementations but this is the most basic I can get to.
class MyList:
def __init__(self):
""" Create the list header record, initialised to an empty list. """
self.top = None
self.bottom = None
self.curr = None # Used to iterate through the list.
def append(self, data):
node = MyListItem(data) # Create the List item
if self.top is None: # If the list is empty point top to node
self.top = node
else:
self.bottom.link_to(node) # Otherwise point the bottom node to the new node
self.bottom = node # Point the bottom to the new node
def __iter__(self):
self.curr = self.top # Initialise the current pointer to top
return self
def __next__(self):
if self.curr: # If the curr pointer is not None
res = self.curr.data # Get the data
self.curr = self.curr.next # Set curr to next
return res # Return the data
else:
raise StopIteration
Test it
test = MyList()
test.append(1)
test.append('Two')
test.append([1, 2, 3])
for node in test:
print(node)
1
Two
[1, 2, 3]
Context:
Some code I found which implements a XOR linked list. In XOR linked list, instead of each node having a next pointer, it has a both attribute which is the XOR of prev and next node.
import ctypes
# This is hacky. It's a data structure for C, not python.
class Node(object):
def __init__(self, val):
self.val = val
self.both = 0
class XorLinkedList(object):
def __init__(self):
self.head = self.tail = None
self.__nodes = [] # This is to prevent garbage collection
def add(self, node):
if self.head is None:
self.head = self.tail = node
else:
self.tail.both = id(node) ^ self.tail.both
node.both = id(self.tail)
self.tail = node
# Without this line, Python thinks there is no way to reach nodes between
# head and tail.
self.__nodes.append(node)
def get(self, index):
prev_id = 0
node = self.head
for i in range(index):
next_id = prev_id ^ node.both
if next_id:
prev_id = id(node)
node = _get_obj(next_id)
else:
raise IndexError('Linked list index out of range')
return node
def _get_obj(id):
return ctypes.cast(id, ctypes.py_object).value
Questions:
Don't understand the need of _get_obj() function and what it is trying
to do here?
How is self.__nodes = [] useful? And how it implements
garbage collection here?
I have no idea what the following code is doing:
# Without this line, Python thinks there is no way to reach nodes between
# head and tail.
self.__nodes.append(node)`
I can answer most of the sub-questions within your question.
the _get_obj() function is the inverse Python's own id() function (with the CPython interpreter anyway).
There are other ways to do it. See for example the question Is it possible to dereference variable id's?.
& 3. The self.__nodes.append(node) adds the Node instance to a private list because adding it to the XOR linked-list doesn't create a reference to it as would likely happen in a more common normal implementation (the XOR trick eliminates the need for them). Without this, the Python garbage-collector might delete the Node instance while it was still part of the linked-list.
I'm new to python and am trying to return the preorder list of an ordered tree (NOTE: Not Binary Tree). I'm having some trouble following the recursion after it reaches a leaf of the tree. How do I get it to go back up to the previous node? Here's my code thus far:
def OrdPreOrder(T):
if Is_OrdLeaf(T):
return []
else:
for t in Subtrees(T):
return [OrdRoot(t)] + OrdPreOrder(t)
Thanks in advance,
The question is not very clear to me, but hopefully this will help.
You want to do a pre-order traversal of an ordered tree.
Pre-Order traversal means
1. Firstly print the value stored in node
2. Then print the value stored in children (according to some principle)
First off,
How do I get it to go back up to the previous node?
According to the definition of pre-order traversal i have written above, I don't see why you need to go back and revisit the parent node.
class Node:
def __init__(self, data):
self.__data = data
self.__children = []
def identifier(self):
return self.__data
def children(self):
return self.__children
def add_child(self, data):
self.__children.append(data)
class Tree:
def __init__(self):
self.__nodes = {}
def nodes(self):
return self.__nodes
def add_node(self, data, parent=None):
node = Node(data)
self[data] = node
if parent is not None:
self[parent].add_child(data)
return node
def traversal(tree):
if tree == None:
return
print (tree.identifier())
for child in tree.children():
traversal(child)
I am also not that well versed with data structures in Python (there might be mistakes in the code). But hopefully it might point you in the right direction.
I programmed a Trie as a class in python. The search and insert function are clear, but now i tried to programm the python function __str__, that i can print it on the screen. But my function doesn't work!
class Trie(object):
def __init__(self):
self.children = {}
self.val = None
def __str__(self):
s = ''
if self.children == {}: return ' | '
for i in self.children:
s = s + i + self.children[i].__str__()
return s
def insert(self, key, val):
if not key:
self.val = val
return
elif key[0] not in self.children:
self.children[key[0]] = Trie()
self.children[key[0]].insert(key[1:], val)
Now if I create a Object of Trie:
tr = Trie()
tr.insert('hallo', 54)
tr.insert('hello', 69)
tr.insert('hellas', 99)
And when i now print the Trie, occures the problem that the entries hello and hellas aren't completely.
print tr
hallo | ellas | o
How can i solve that problem?.
Why not have str actually dump out the data in the format that it is stored:
def __str__(self):
if self.children == {}:
s = str(self.val)
else:
s = '{'
comma = False
for i in self.children:
if comma:
s = s + ','
else:
comma = True
s = s + "'" + i + "':" + self.children[i].__str__()
s = s + '}'
return s
Which results in:
{'h':{'a':{'l':{'l':{'o':54}}},'e':{'l':{'l':{'a':{'s':99},'o':69}}}}}
There are several issues you're running into. The first is that if you have several children at the same level, you'll only be prefixing one of them with the initial part of the string, and just showing the suffix of the others. Another issue is that you're only showing leaf nodes, even though you can have terminal values that are not at a leaf (consider what happens when you use both "foo" and "foobar" as keys into a Trie). Finally, you're not outputting the values at all.
To solve the first issue, I suggest using a recursive generator that does the traversal of the Trie. Separating the traversal from __str__ makes things easier since the generator can simply yield each value we come across, rather than needing to build up a string as we go. The __str__ method can assemble the final result easily using str.join.
For the second issue, you should yield the current node's key and value whenever self.val is not None, rather than only at leaf nodes. As long as you don't have any way to remove values, all leaf nodes will have a value, but we don't actually need any special casing to detect that.
And for the final issue, I suggest using string formatting to make a key:value pair. (I suppose you can skip this if you really don't need the values.)
Here's some code:
def traverse(self, prefix=""):
if self.val is not None:
yield "{}:{}".format(prefix, self.val)
for letter, child in self.children.items():
yield from child.traverse(prefix + letter)
def __str__(self):
return " | ".join(self.traverse())
If you're using a version of Python before 3.3, you'll need to replace the yield from statement with an explicit loop to yield the items from the recursive calls:
for item in child.traverse(prefix + letter)
yield item
Example output:
>>> t = Trie()
>>> t.insert("foo", 5)
>>> t.insert("bar", 10)
>>> t.insert("foobar", 100)
>>> str(t)
'bar:10 | foo:5 | foobar:100'
You could go with a simpler representation that just provides a summary of what the structure contains:
class Trie:
def __init__(self):
self.__final = False
self.__nodes = {}
def __repr__(self):
return 'Trie<len={}, final={}>'.format(len(self), self.__final)
def __getstate__(self):
return self.__final, self.__nodes
def __setstate__(self, state):
self.__final, self.__nodes = state
def __len__(self):
return len(self.__nodes)
def __bool__(self):
return self.__final
def __contains__(self, array):
try:
return self[array]
except KeyError:
return False
def __iter__(self):
yield self
for node in self.__nodes.values():
yield from node
def __getitem__(self, array):
return self.__get(array, False)
def create(self, array):
self.__get(array, True).__final = True
def read(self):
yield from self.__read([])
def update(self, array):
self[array].__final = True
def delete(self, array):
self[array].__final = False
def prune(self):
for key, value in tuple(self.__nodes.items()):
if not value.prune():
del self.__nodes[key]
if not len(self):
self.delete([])
return self
def __get(self, array, create):
if array:
head, *tail = array
if create and head not in self.__nodes:
self.__nodes[head] = Trie()
return self.__nodes[head].__get(tail, create)
return self
def __read(self, name):
if self.__final:
yield name
for key, value in self.__nodes.items():
yield from value.__read(name + [key])
Instead of your current strategy for printing, I suggest the following strategy instead:
Keep a list of all characters in order that you have traversed so far. When descending to one of your children, push its character on the end of its list. When returning, pop the end character off of the list. When you are at a leaf node, print the contents of the list as a string.
So say you have a trie built out of hello and hellas. This means that as you descend to hello, you build a list h, e, l, l, o, and at the leaf node you print hello, return once to get (hell), push a, s and at the next leaf you print hellas. This way you re-print letters earlier in the tree rather than having no memory of what they were and missing them.
(Another possiblity is to just descend the tree, and whenever you reach a leaf node go to your parent, your parent's parent, your parent's parent's parent... etc, keeping track of what letters you encounter, reversing the list you make and printing that out. But it may be less efficient.)