This question broken into subquestions:
- Pointers in Python suggested by one reply to look at, more here
- "why not to modify locals?" -question here.
Original Question
#!/usr/bin/python
#
# Description: trying to evaluate array -value to variable before assignment
# but it overwrites the variable
#
# How can I evaluate before assigning on the line 16?
#Initialization, dummy code?
x=0
y=0
variables = [x, y]
data = ['2,3,4', '5,5,6']
# variables[0] should be evaluted to `x` here, i.e. x = data[0], how?
variables[0] = data[0]
if ( variables[0] != x ):
print("It does not work, why?");
else:
print("It works!");
Goal: to fix hard-coded assignments on a lab-report, before I can use list-comprehensions more effectively I need to fix the assignment problem -- or am I doing something wrong?
#!/usr/bin/python
#
# Description: My goal is to get the assignments on the line 24 not-hard-coded
variables = [y, x, xE]
files = [measurable, time, timeErr]
# PROBLEM 1: Assignment problem
#
#Trying to do:
#
# var[1] = files[1] so that if (y == measurable): print("it works")
# var[2] = files[2] so that if (x == time): print("it works")
#GOAL TO GET ASSIGNMENT LIKE, data is in files "y, x, xE":
#
# y = [3840,1840,1150,580,450,380,330,340,340,2723]
# x = [400.0,204.1,100.0,44.4,25.0,16.0,11.1,8.2,7.3,277.0]
# xE = [40, 25, 20, 20, 20, 20, 20, 20, 20, 35]
#Trying to do this
#
# y = open('./y').read();
# x = open('./x').read();
# xE= open('./xE').read();
#
# like this? f evaluated each time?
for f in files:
f = open('./'+f).read()
I would use a dictionary with a bit of indirection thrown in. When you want to use dynamic variable names that's a hint that you should not be using actual variables but instead some sort of data structure like a list or a dict.
Try using two data structures: a list called
variables
which stores the names of the variables, and a dict calledvalues
which stores their values.Your problem is that in your initialization of variables, variables[0] become 0, the value of x. And then you are over-writing that value with a new value.
The following code outputs 0, not 1.
You can get the desired result by wrapping x in an array and dereferencing.
Your confusing pointers with references.
x=0
variables = [x, y]
this will not matter since its going to get reassignedvariables[0] = data[0]
so variables[0] == ['2,3,4]at 3 you are thinking you have a reference to x. But you do not! You only the have the reference to variables[0]
Maybe your thinking of something like this:
This will actually change the value of x. Lists and dictionaries will change (as in
mutatable
) while strings and numbers will not. Thre is no way to get a reference to them to change the underlying value.[Partial Solution]
I think the Gerrat correctly uses locals, the limitations here, because some of the data in lab reports will never change.
[SOLUTION]
jonesy and John Kugelman spot other problem, you need to use dynamic data structrures such as dictionary, a cleaned example below.
[Solution 2] by jonesy, it is actually the clearest code.
You've got a basic idea of how references work but you're getting confused. When you call the statement
variables[0] = data[0]
instead of reassigning whatx
points to you reassign whatvariables[0]
points to. That's why "it does not work."Here:
which is at the heart of what you're attempting to do. If you type this into the REPL you'll see that
x
still equates to 0, not 4.It looks like you're just overthinking the problem. Think about what actual outcome you want to have. This code is so confused that I'm not even sure what the actual problem is you're trying to solve. I see one of two possibilities (that seem reasonable - there are infinite unreasonable ones):
You want to assign x = '2,3,4' and y = '5,5,6', but you don't have the values at the time you initialize x and y. In this case, you'd do:
You want a dictionary with keys x and y with the corresponding values from data, in which case you'd do: