如何在分配之前将变量评估为另一个变量?

时间:2011-01-04 03:30:37

标签: python variable-assignment

这个问题分为子问题

  • Python中的指针由一个回复建议查看,更多here
  • “为什么不修改当地人?” -question here.

原始问题

#!/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!");

目标:修复实验室报告中的硬编码分配,在我更有效地使用列表推导之前,我需要修复分配问题 - 或者我做错了什么? / p>

#!/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()

7 个答案:

答案 0 :(得分:2)

我会使用一个带有一点间接性的字典。如果你想使用动态变量名,那就是你不应该使用实际变量,而是某种数据结构,如列表或字典。< / p>

尝试使用两种数据结构:一个名为variables的列表,用于存储变量的名称,以及一个名为values的dict,用于存储

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()

答案 1 :(得分:1)

您已经了解了引用的工作方式,但您却感到困惑。当您调用语句variables[0] = data[0]而不是重新分配x指向您重新分配variables[0]指向的内容时。这就是为什么“它不起作用。”

下面:

x = 0
y = x
y = 4

这是您尝试做的事情的核心。如果您在REPL中输入此内容,您会看到x仍然等于0,而不是4。

答案 2 :(得分:1)

你的引用令人困惑的指针。

  1. x=0
  2. variables = [x, y]这将无关紧要,因为它将被重新分配
  3. variables[0] = data[0]所以变量[0] == ['2,3,4]
  4. 在3你认为你有x的引用。但你没有!您只能引用变量[0]

    也许你想到这样的事情:

    x = [1]
    var = [x]
    var[0][0] = 2
    print var, x
    >>> [[2]] [2]
    

    这实际上会改变x的值。列表和词典将更改(如mutatable),而字符串和数字则不会。没有办法获得对它们的引用来改变基础价值。

答案 3 :(得分:1)

看起来你只是在思考这个问题。想想你想要的实际结果。这段代码很混乱,我甚至不确定你要解决的实际问题是什么。我看到两种可能性中的一种(这似乎是合理的 - 有无限的不合理的):

  1. 您想要指定x ='2,3,4'和y ='5,5,6',但是在初始化x和y时没有值。在这种情况下,您可以:

    data = []
    ... data is eventually populated ...
    x, y = data
    
  2. 您想要一个带有键x和y的字典,其中包含数据中的相应值,在这种情况下,您需要:

    klist = ['x', 'y']
    data = ['2,3,4', '5,5,6']
    mydict = dict(zip(klist, data))
    # mydict['x'] == '2,3,4'
    

答案 4 :(得分:1)

......啊,我想我得到了你想要的东西。有99%的可能性这不是一个好主意,但我会给你代码来做我认为你要求的事情:

首先,您需要更改此行:

variables = ['x','y']

(因为你想要这里的变量名,而不是值)

现在为了分配变量x,一种方法是:

locals()[variables[0]] = data[0]

答案 5 :(得分:0)

你的问题是,在初始化变量时,变量[0]变为0,x的值。然后你用一个新值来覆盖那个值。

以下代码输出0,而不是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]);

通过将x包装在数组中并取消引用,可以获得所需的结果。

#!/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!");

答案 6 :(得分:0)

[部分解决方案]

我认为Gerrat正确使用了本地人,限制here,因为实验室报告中的某些数据永远不会改变。

#!/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'");    

[解决方案]

jonesy和John Kugelman发现其他问题,你需要使用动态数据结构,如字典,下面是一个清理过的例子。

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]);

[解决方案2] jonesy,它实际上是最清晰的代码。

klist = ['x', 'y']
data = ['2,3,4', '5,5,6']
mydict = dict(zip(klist, data))
# mydict['x'] == '2,3,4'