LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2009, 02:42 PM   #1
tadeas
Member
 
Registered: Jul 2008
Location: Prague
Distribution: Opensuse
Posts: 40

Rep: Reputation: 5
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]?
 
Old 03-21-2009, 04:28 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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

Last edited by Hko; 03-21-2009 at 04:30 PM.
 
Old 03-22-2009, 05:16 AM   #3
tadeas
Member
 
Registered: Jul 2008
Location: Prague
Distribution: Opensuse
Posts: 40

Original Poster
Rep: Reputation: 5
Very well, thank you for the explanation! Now it works fine..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 01:50 PM
procmail problem (nested recipes) fakie_flip Linux - Software 0 12-24-2007 10:56 PM
python: differences between dictionaries and lists tjyorkshire Programming 5 08-12-2007 04:40 AM
[Python]Logical problem working with lists Caesar Tjalbo Programming 3 12-27-2006 03:58 AM
Accessing nested dictionaries in Python DiagonalArg Programming 13 08-20-2005 08:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration