I am absolutely useless with python and I'm struggling to do what seems to be simple things. I need to read a text file which contains a network routing table which contains the distance between each node on the network (below)
0,2,4,1,6,0,0
2,0,0,0,5,0,0
4,0,0,0,0,5,0
1,0,0,0,1,1,0
6,5,0,1,0,5,5
0,0,5,1,5,0,0
0,0,0,0,5,0,0
I then need to assign it to a two dimensional array which I have done with the code i have written below..
Network = []
NodeTable = []
def readNetwork():
myFile = open('network.txt','r')
for line in myFile.readlines():
line.strip(' \n' '\r')
line = line.split(',')
line = [int(num) for num in line]
Network.append(line)
Once that has been done I then need to iterate through the Network array and add each horizontal line to another array which will hold information about the nodes, but as far as I have been able to get with that is here:
class Node(object):
index = #Needs to start from A and increase with each node
previousNode = invalid_node
distFromSource = infinity
visited = False
NodeTable.append(Node())
So that array will be initialised as:
A invalid_node infinity False
B invalid_node infinity False
C invalid_node infinity False
...
etc
Could anyone give me a hand with creating each node in the NodeTable array?
Strings in Python are immutable, thus with the following line:
line.strip(' \n' '\r')
you are only getting a copy of the line string, stripped of some characters, but you do not assign it to anything. Change it into:
line = line.strip(' \n' '\r')
As DSM pointed out in the comments, it will not change much, as int will just ignore redundant whitespaces.
intsYou also are mapping strings to ints like that:
line = [int(num) for num in line]
which could be replaced by clearer:
line = map(int, line)
and should give you some slight performance gain also. To shorten your code, you can also replace the following lines:
line.strip(' \n' '\r')
line = line.split(',')
line = [int(num) for num in line]
Network.append(line)
with the following:
Network.append(map(int, line.split(',')))
Node's index attribute with each instanceThis could be done like that:
>>> class Node(object):
baseindex = '@' # sign before "A"
def __init__(self):
cls = self.__class__
cls.baseindex = chr(ord(cls.baseindex) + 1)
self.index = self.baseindex
self.previousNode = 'invalid_node'
self.distFromSource = 'infinity'
self.visited = False
>>> a = Node()
>>> a.index
'A'
>>> b = Node()
>>> b.index
'B'
>>> a.index
'A'
As you can see baseindex is attached to the class, and index is attached to the class's instance. I suggest you should attach every instance-specific variable to the instance, as shown in the example above.
Node into the list as listOne of the easiest ways to insert it as list into another list, is to add a method returning it as list (see as_list() method):
>>> class Node(object):
baseindex = '@' # sign before "A"
def __init__(self):
cls = self.__class__
cls.baseindex = chr(ord(cls.baseindex) + 1)
self.index = self.baseindex
self.previousNode = 'invalid_node'
self.distFromSource = 'infinity'
self.visited = False
def as_list(self):
return [self.index, self.previousNode, self.distFromSource,
self.visited]
>>> a = Node()
>>> a.index
'A'
>>> a.as_list()
['A', 'invalid_node', 'infinity', False]
so you should be able to add nodes like this:
NodeTable.append(Node().as_list())
But remember - after doing the above, you will not get list of Node instances, you will get list of lists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With