LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python and nested lists problem (https://www.linuxquestions.org/questions/programming-9/python-and-nested-lists-problem-713424/)

tadeas 03-21-2009 02:42 PM

Python and nested lists problem
 
I have a matrix with 5 rows and 4 cols containing some numbers. I need to take each row, do an average from the first and second col, and another average from the third and fourth col, subtract these numbers from a constant and put these in another matrix, which will then have 5 rows and 2 cols.
Here's the code that I coined up, but it counts nonsenses:
Code:

vrch=624
pruz=[
    [511,512,392,394],
    [504,503,424,424],
    [469,468,439,440],
    [472,473,446,447],
    [517,517,496,496]]
delky=[[0,0],[0,0],[0,0],[0,0],[0,0]]
ins=[0,0]

i=0
# I count the final numbers
for row in pruz:
    ins[0]=vrch-(row[0]+row[1])/float(2)
    ins[1]=vrch-(row[2]+row[3])/float(2)
    delky[i]=ins # and put them into the second matrix (delky)
    print i,delky[i] # now I print it and it looks fine
    i+=1

# However when I try to print it again, I get wrong numbers
print
for row in delky:
    print row

The first cycle seems to be working fine, however the problem must be there. I print the information twice, first it's allright (from the firs cycle) and the second time I get only the last row of the new matrix five times.

Here's the output:
Code:

0 [112.5, 231.0]
1 [120.5, 200.0]
2 [155.5, 184.5]
3 [151.5, 177.5]
4 [107.0, 128.0]

[107.0, 128.0]
[107.0, 128.0]
[107.0, 128.0]
[107.0, 128.0]
[107.0, 128.0]

Any ideas where is the mistake? Why does delky[0:3] contain the same information as delky[4]?

Hko 03-21-2009 04:28 PM

In the loop you are assigning the list ins to all rows in delky.

Since a list is an object (not a primitive type, like e.g. an integer), all delky[i] will become a reference to ins. But the contents of ins change each time the loop runs. So all delky[i] will refer to the same ins!

A bit difficult to explain. But try this smaller program and see what happens in the last part:
Code:

delky = [[0,0], [0,0], [0,0]]
ins = [1,2]

delky[0] = ins
delky[1] = ins
delky[2] = ins
print delky

print "now changing 'ins' by index (only once):"
ins[0] = 555
ins[1] = 666
print delky

On solution is to make sure a new list ins is created each time the loop runs ins is assigned to delky[i]:
Code:

delky = [[0,0], [0,0], [0,0]]
ins = [1,2]

delky[0] = list(ins)
delky[1] = list(ins)
delky[2] = list(ins)
print delky

print "now changing 'ins' by index (only once):"
ins[0] = 555
ins[1] = 666
print delky

So, to fix your code:
Code:

vrch=624
pruz=[
    [511,512,392,394],
    [504,503,424,424],
    [469,468,439,440],
    [472,473,446,447],
    [517,517,496,496]]
delky=[[0,0],[0,0],[0,0],[0,0],[0,0]]
ins=[0,0]

i=0
# I count the final numbers
for row in pruz:
    ins[0]=vrch-(row[0]+row[1])/float(2)
    ins[1]=vrch-(row[2]+row[3])/float(2)
    delky[i]=list(ins) # and put them into the second matrix (delky)
    print i,delky[i] # now I print it and it looks fine
    i+=1

# However when I try to print it again, I get wrong numbers
print
for row in delky:
    print row

However, I would prefer to write your program like this (no need for ins and i):
Code:

vrch=624
pruz=[
    [511,512,392,394],
    [504,503,424,424],
    [469,468,439,440],
    [472,473,446,447],
    [517,517,496,496]]
delky=[]

for row in pruz:
    AAA = vrch-(row[0]+row[1])/float(2)
    BBB = vrch-(row[2]+row[3])/float(2)
    delky.append([AAA,BBB])

for row in delky:
    print row


tadeas 03-22-2009 05:16 AM

Very well, thank you for the explanation! Now it works fine..


All times are GMT -5. The time now is 11:34 AM.