Nested List Doesn't Work Properly
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.0 def ge
Solution 1:
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 copyimport 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.
Solution 2:
This line:
equation=eqn
doesn't do what you think it does. Try this instead:
importcopy
equation=copy.deepcopy(eqn)
Python assignment isn't a copy operation, but rather a binding operation. The line you have means "bind the name equation
to the same object to which eqn
is currently bound."
Your algorithm requires that equation
and eqn
are distinct objects.
Post a Comment for "Nested List Doesn't Work Properly"