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

  • Home
  • SEARCH
  • 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 4116638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:44:00+00:00 2026-05-20T22:44:00+00:00

We made a little script in computer science class for finding the shortest path

  • 0

We made a little script in computer science class for finding the shortest path through a grid of nodes, some interlinked, some not.
The griddata is stored in a way that doesn’t require to define any new classes, namely in a nested list. For n nodes, the list should have n elements, each being a list with n elements.

grid[a][b] should return the distance from node a to node b, and 0 if the nodes aren’t connected or if a and b references to the same node.

for example the list [[0,2,2],[2,0,2],[2,2,0]] would define a grid with 3 nodes, each with a distance of 2 from each other.

Here is the script I wrote that should define such a grid by requesting individual distances from the user:

def makegrid(nodes):
    res=[[0]*nodes]*nodes
    for i in range(nodes):
        for j in range(nodes):
            if res[i][j]==0 and not i==j:
                x=raw_input('distance node %d zu node %d: ' % (i,j))
                if x:
                    res[i][j]=res[j][i]=int(x)
                else:
                    res[i][j]=res[j][i]=None
    for i in range(nodes):
        for j in range(nodes):
            if res[i][j] is None:
                res[i][j]=0
    return res

My problem ist that the function isn’t quite doing what I intended it to do, and I can’t seem to work out why. What it actually does is only running through the requests for the first node, and somehow it does to all the sublists what it should only do to the first (and ignoring that the first element should not be changed).

Can anyone help me figure it out?

  • 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-20T22:44:01+00:00Added an answer on May 20, 2026 at 10:44 pm
     res=[[0]*nodes]*nodes
    

    probably doesn’t do what you think it does:

    >>> nodes = 3
    >>> res=[[0]*nodes]*nodes
    >>> res
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    >>> res[0][0] = 'echo'
    >>> res
    [['echo', 0, 0], ['echo', 0, 0], ['echo', 0, 0]]
    

    Instead of making three separate lists [0,0,0], it gives you three references to the same list:

    >>> id(res[0]), id(res[1]), id(res[2])
    (4299671960, 4299671960, 4299671960)
    

    Try

    res=[[0]*nodes for i in range(nodes)]
    

    instead, and read, e.g., Multiply operator applied to list(data structure) to understand why the difference in mutability of integers and lists explains why the [0]*nodes part seems to behave like you’d expect — i.e. why the above answer has multiple copies of [‘echo’, 0, 0], not [‘echo’, ‘echo’, ‘echo’] — even though the final “*nodes” doesn’t.

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

Sidebar

Related Questions

I made a little PHP script that checks if an email is valid. The
Im having a little trouble with a php script ive made. you can see
I made some little programs with Swing-components in JRuby. Now I want to convert
I've made a little inline editor with some clean up logic. So if you
So... I have some little flash graphs I made that pull data from an
I am working on little script and made wrong decision to do it in
i've made a little script to calculate prime numbers, i'm using the latest xampp
For a site i made, http://crystalmurray.com , i have a simple little script to
I have a little script to read in, parse and derive some kind of
I've made a little script to convert titles into url friendly things. ie: '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.