Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:24:13+00:00 2026-05-28T20:24:13+00:00

I am absolutely useless with python and I’m struggling to do what seems to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:24:14+00:00Added an answer on May 28, 2026 at 8:24 pm

    Redundant line

    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.

    Mapping string to ints

    You 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(',')))
    

    How to increase Node‘s index attribute with each instance

    This 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.

    Adding Node into the list as list

    One 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I absolutely need to use an IoC container for decoupling dependencies in an ever
I absolutely loved Dive Into Python when I picked up Python. In fact, tutorials
I have an absolutely positioned div containing several children, one of which is a
I absolutely hate RegEx, I really need to learn it - it's so powerful.
I have an absolutely positioned div containing several children, one of which is a
I need to figure out which part of a linux program that I am
Absolutely basic Java question which I'm having a hard time finding on Google. What
CSS file: http://useless-r-us.t15.org/projects/war/stylesheet.css JAVASCRIPT file: http://useless-r-us.t15.org/projects/war/engine.js The game itself (html code if you view
I absolutely need an extern alias for System.Core in my project. Unfortunately, in a
I absolutely love the Keep Remote Directory Up-to-date feature in Winscp . Unfortunately, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.