Nested list doesnt work properly

2024/7/7 6:46:08
import re
def get_number(element):re_number = re.match("(\d+\.?\d*)", element)if re_number:return float(re_number.group(1))else:return 1.0def getvalues(equation):elements = re.findall("([a-z0-9.]+)", equation)return [get_number(element) for element in elements]eqn = []
eqn_no = int(raw_input("Enter the number of equations: "))for i in range(eqn_no):eqn.append(getvalues(str(raw_input("Enter Equation %d: " % (i+1)))))
print "Main Matrix: "
for i in range((eqn_no)):for j in range((eqn_no+1)):print "\t%f" %(eqn[i][j]),print
print
equation=[]
equation=eqn
for k in range((eqn_no-1)):for i in range((k+1),eqn_no):for j in range((eqn_no+1)):if(eqn[i][j]!=0):eqn[i][j]=eqn[i][j]-(eqn[k][j]*(equation[i][k]/eqn[k][k]))print "Matrix After %d step: " %(k+1)for i in range(eqn_no):for j in range((eqn_no+1)):print "\t%f"%(eqn[i][j]),equation[i][j]=eqn[i][j];printprint

for input:

25x+5y+z=106.8
64x+8y+z=177.2
144x+12y+z=279.2

output is:

Main Matrix: 25.000000   5.000000    1.000000    106.80000064.000000   8.000000    1.000000    177.200000144.000000  12.000000   1.000000    279.200000Matrix After 1 step: 25.000000   5.000000    1.000000    106.8000000.000000    8.000000    1.000000    177.2000000.000000    12.000000   1.000000    279.200000Matrix After 2 step: 25.000000   5.000000    1.000000    106.8000000.000000    8.000000    1.000000    177.2000000.000000    0.000000    1.000000    279.200000

But it should be like

Main Matrix: 25.000000   5.000000    1.000000    106.80000064.000000   8.000000    1.000000    177.200000144.000000  12.000000   1.000000    279.200000Matrix After 1 step: 25.000000   5.000000    1.000000    106.8000000.000000    -4.80000    -1.56000    -96.2080000.000000    -16.8000    -4.76000    -335.968000Matrix After 2 step: 25.000000   5.000000    1.000000    106.8000000.000000    -4.80000    -1.56000    -96.2080000.000000    0.000000    0.699999    0.759981

First of all this is a partial code for solving root of n number of equations using Naive Guass elemination method. Does anyone have any idea why the hell on earth is this happening? Why the zero parts are changing and others aren't? I have done this code in c++ and it works there perfectly but here I'm facing many problem. Maybe I'm newbie to python. I'm using python 2.7.....

Answer

I think the problem is the assignment equation = eqn. Since eqn is a list, it is a mutable and thus passed as a reference, when you assign a mutable, the variable actually contains a pointer to that object. This means that equation and eqn are the same list.

You should

from copy import deepcopy
equation = deepcopy(eqn)

You need deepcopy instead of copy because you have a list of lists, also the inner list needs to be copied.

https://en.xdnf.cn/q/120379.html

Related Q&A

How can I increment array with loop?

Ive got two lists:listOne = [33.325556, 59.8149016457, 51.1289412359] listTwo = [2.5929778, 1.57945488999, 8.57262235411]I use len to tell me how many items are in my list:itemsInListOne = int(len(list…

Two Complements Python (with as least bits as possible)

I am trying to output the binary representation of an negative number with the least bytes available each time.Example:-3 -> 101 -10 -> 10110

iterating through a column in dataframe and creating a list with name of the column + str

I have a dataframe with 2 coulmn, i want to iterate through the column headers, use that value and add a string to it and use it as the name of a list.rrresampled=pd.DataFrame() resampled[RAT]=dd1[RAT]…

Python multiple inheritance name clashes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

How to read multiple txt file from a single folder in Python? [duplicate]

This question already has answers here:How to open every file in a folder(8 answers)Closed 3 years ago.How can I read multiple txt file from a single folder in Python?I tried with the following code b…

Time Changing Without Clicking Button

The following code works fine as long as the time in the spinbox does not change. What I want is to do the set the time for a break. The first time it works perfectly but after that if I change the val…

Avoid `logger=logging.getLogger(__name__)` without loosing way to filter logs

I am lazy and want to avoid this line in every python file which uses logging:logger = logging.getLogger(__name__)In january I asked how this could be done, and found an answer: Avoid `logger=logging.g…

Find all directories and files of your laptop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

How to format and write excel using pandas

Lets assume that we have nested python dictionaries which should be written in single excel sheet file. Following are sample dictionaries which can be used. Car = [{"carbmodel": "Model A…

Openpyxl is unable to read after modifying

Requirement : 1.create a gui using Tkinter 2.Update the excel by fetching values from Tkinter entry widget 3.Read another sheet of the same workbook 4.plot graph using inside the Tkinter window.Prob…