可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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()
回答1:
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:
data = []
... data is eventually populated ...
x, y = data
You want a dictionary with keys x and y with the corresponding values from data, in which case you'd do:
klist = ['x', 'y']
data = ['2,3,4', '5,5,6']
mydict = dict(zip(klist, data))
# mydict['x'] == '2,3,4'
回答2:
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 called values
which stores their values.
variables = ['y', 'x', 'xE']
values = dict((name, None) for name in variables)
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")
values[variables[1]] = files[1]
values[variables[2]] = files[2]
if values['y'] == measurable:
print("it works")
# GOAL TO GET ASSIGNMENT LIKE, data is in files "y, x, xE":
for name in variables:
variables[name] = open('./'+name).read()
回答3:
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 what x
points to you reassign what variables[0]
points to. That's why "it does not work."
Here:
x = 0
y = x
y = 4
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.
回答4:
...ah, I think I get what you want. There is a 99% probability that this is not a good idea, but I will give you the code to do what I think you're asking for anyway:
First you need to change this line:
variables = ['x','y']
(since you want the variable names here, not the values)
Now in order for the variable x
to be assigned, one way to do that is:
locals()[variables[0]] = data[0]
回答5:
Your confusing pointers with references.
x=0
variables = [x, y]
this will not matter since its going to get reassigned
variables[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:
x = [1]
var = [x]
var[0][0] = 2
print var, x
>>> [[2]] [2]
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.
回答6:
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.
#!/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']
x = 1
variables[0] = data[0]
print(variables[0]);
You can get the desired result by wrapping x in an array and dereferencing.
#!/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][0] = data[0]
if ( variables[0][0] != x[0] ):
print("It does not work, why?");
else:
print("It works!");
回答7:
[Partial Solution]
I think the Gerrat correctly uses locals, the limitations here, because some of the data in lab reports will never change.
#!/usr/bin/python
#
# Description: smelling overuse, probably a roundabout of other problem, rethinking...
variables = ['x','y']
datas = ['1,2,3,4', '4,44,8,3']
for var, data in zip(variables, datas):
locals()[var] = data
#Testing
print(x +" should be '1,2,3,4'");
print(y +" should be '4,44,8,3'");
[SOLUTION]
jonesy and John Kugelman spot other problem, you need to use dynamic data structrures such as dictionary, a cleaned example below.
variables = ['y', 'x', 'xE']
values = dict((name, None) for name in variables)
# GOAL TO GET ASSIGNMENT LIKE, data is in files "y, x, xE":
for name in variables:
values[name] = open('./'+name).read()
# Testing, prints the contents
for val in variables:
print(values[val]);
[Solution 2] by jonesy, it is actually the clearest code.
klist = ['x', 'y']
data = ['2,3,4', '5,5,6']
mydict = dict(zip(klist, data))
# mydict['x'] == '2,3,4'