【发布时间】:2011-01-04 03:30:37
【问题描述】:
这个问题分成子问题:
原始问题
#!/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!");
目标:修复实验室报告上的硬编码作业,在我可以更有效地使用列表理解之前,我需要解决作业问题 - 还是我做错了什么?
#!/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()
【问题讨论】:
-
目前还不清楚您要在这里寻找什么。 variables[0] = data[0] 表示现在 variables == ['2,3,4', 0];或变量 [0] = '2,3,4'。您将
x分配给 0...他们现在怎么可能相等? -
您在这里的真正目标是什么?您尝试做的事情是困难且笨拙且编码不好。几乎可以肯定,有更好的方法来实现你的真正目标。
-
你的 if 结构不是正确的 Python 语法。
-
John Kugelman:很抱歉造成混乱,您现在可以看到一般问题。我基本上有很多从变量到文件的双射关系,我想批量初始化分配它们。你会怎么做?
标签: python variable-assignment