ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 sameins!
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]:
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.