Unexpected output for list

This is the place for queries that don't fit in any of the other categories.

Unexpected output for list

Postby scottyiu » Tue Jan 07, 2014 5:54 pm

Hello

First time posting here. I am a new user to Python. I am puzzled to why the list 'matrix' is changed between the two print statements (the ones with 'Hi' and 'Hi2') even though I did nothing to change it in the code. Below is the code:

Code: Select all
#FIRST PROPER SCRIPT
import math
number = int(raw_input("What number do you want to input?\n"))

from random import randint

#Declaring variables
matrix = []
hold1 = [0 for x in xrange(4)]
count = 0

while (1 != 0):
    count = count + 1
    a = randint(0,int(math.sqrt(number)))
    b = randint(0,int(math.sqrt(number)))
    c = randint(0,int(math.sqrt(number)))
    d = randint(0,int(math.sqrt(number)))
   
    if ((a**2)+(b**2)+(c**2)+(d**2)) == number:
        print('Hi' + str(matrix))
        hold1[0] = a
        hold1[1] = b
        hold1[2] = c
        hold1[3] = d
        hold1.sort()
        print('Hi2' + str(matrix))
       
        if len(matrix) == 0:
            #store
            matrix.append(hold1)


Below is the output:

Code: Select all
What number do you want to input?
5462
Hi[]
Hi2[]
Hi[[4, 19, 27, 66]]
Hi2[[7, 7, 42, 60]]
Hi[[7, 7, 42, 60]]
Hi2[[21, 27, 34, 56]]
Hi[[21, 27, 34, 56]]
Hi2[[7, 30, 47, 48]]


I expected each pair of Hi and Hi2 to have the exact same output. What have I done wrong?
Last edited by stranac on Tue Jan 07, 2014 6:10 pm, edited 1 time in total.
Reason: First post lock.
scottyiu
 
Posts: 3
Joined: Tue Jan 07, 2014 5:45 pm

Re: Unexpected output for list

Postby stranac » Tue Jan 07, 2014 6:24 pm

The only thing you have in matrix is a list(you append hold1 to it).
At this point, matrix[0] and hold1 are just two different names for the same list.
If you change the list using one of its names, the changes will be visible no matter which name you use:
Code: Select all
>>> a = [1, 2, 3]
>>> # give a another name
... b = a
>>> b.append(4)
>>> a
[1, 2, 3, 4]

Hope that helps.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Unexpected output for list

Postby scottyiu » Tue Jan 07, 2014 9:57 pm

Thank you very much. I understand this better now. If I want this to be two lists, would list.copy be the right way to go?
scottyiu
 
Posts: 3
Joined: Tue Jan 07, 2014 5:45 pm

Re: Unexpected output for list

Postby stranac » Tue Jan 07, 2014 10:25 pm

Yes, that would be one possible way(others include slicing and using list()).

But you really don't need a copy here.
You can simply create a new list every time.

You can add elements to a list using .append(), so you don't have to create a 4-element list in advance.
Also, you seem to know about list comprehensions. You could use one of those to create the new list every time.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Unexpected output for list

Postby scottyiu » Sun Jan 12, 2014 8:12 pm

Thank you for your reply. It just seems really strange coming from a c++ background.
scottyiu
 
Posts: 3
Joined: Tue Jan 07, 2014 5:45 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests