1

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()
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
hhh
  • 50,788
  • 62
  • 179
  • 282
  • It's not really clear what you're looking for here. variables[0] = data[0] means that now variables == ['2,3,4', 0]; or that variables[0] = '2,3,4'. you assigned `x` to 0...how would they possibly be equal now? – Gerrat Jan 04 '11 at 03:39
  • 1
    What's your real goal here? The thing you're trying to do is difficult and clumsy and not good coding. There is almost certainly a better way to accomplish your true goal. – John Kugelman Jan 04 '11 at 03:48
  • You if-structure is not correct Pythhon syntax. – Geoffrey Jan 04 '11 at 08:27
  • John Kugelman: Sorry about the confusion, you can see now the general problem. I have basically many bijective relations from variables to files and I want to bulk-init-assign them. How would you do it? – hhh Jan 04 '11 at 08:34

7 Answers7

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()
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

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.

wheaties
  • 35,646
  • 15
  • 94
  • 131
1

Your confusing pointers with references.

  1. x=0
  2. variables = [x, y] this will not matter since its going to get reassigned
  3. 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.

nate c
  • 8,802
  • 2
  • 27
  • 28
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):

  1. 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
    
  2. 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'
    
jonesy
  • 3,502
  • 17
  • 23
0

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!");
Zeki
  • 5,107
  • 1
  • 20
  • 27
0

...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]
Gerrat
  • 28,863
  • 9
  • 73
  • 101
0

[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'
Community
  • 1
  • 1
hhh
  • 50,788
  • 62
  • 179
  • 282