Need help - variable in Python (continued)

This was helpful to kgiannakakis. I faced the problem as below:

a = ['zbc','2.3']
for i in range(0,5):
    exec('E%d=%s' %(i,a[i]))

      

This leads to:

Traceback (most recent call last):
  File "", line 2, in 
    exec ('E% d =% s'% (i, a [i]))
  File "", line 1, in 
NameError: name 'zbc' is not defined
-3


source to share


4 answers


It looks like the code you are creating expands to:

E0=zbc
E1=2.3

      

On the next iteration through the loop, you will get an IndexError because a

it is only two elements long.

So, given the above, you are trying to assign a value zbc

E0

. If zbc

doesn't exist (it doesn't seem to be), you will get a mention of the NameError.

It's hard to determine what you are actually trying to do with this code, so I'm not sure which one to recommend. You can assign lines instead:



exec('E%d="%s"' %(i,a[i]))

      

This will expand to:

E0="zbc"
E1="2.3"

      

You will still get an IndexError because your array a

will not contain 5 elements. This should be an easy solution for you.

+2


source


Good. this code is very strange.

As one such liner, it is not syntactically correct, but I suspect you are missing line breaks for some reason. But then it becomes

a = ['zbc','2.3']
for i in range(0,5): 
    exec('E%d=%s' %(i,a[i]))

      

But this will result in an index error in reference to [i] as shown:



>>> a
['zbc', '2.3']
>>> for i in range(0,5):
...    print a[i]
... 
zbc
2.3
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: list index out of range

      

If you avoid this problem, you will get

exec("E2.3=1")

      

on the second pass through lopp, as well as a syntax error.

0


source


You seem to be trying to use the solution tagged in this question.

If your goal is access values ​​in a loop, you should just use a list. This strange concept of variable names with numbers in them is not one that should be used in any language. Try it.

vals = ['foo', 'bar', 'blah', 67, -0.4, 'your mom']
for i in range(len(vals)):
    print(vals[i])

      

This is the correct way to have a list of values ​​indexed by an integer without putting it in the variable name.

0


source


Just keep in mind that "exec" executes any line you pass as if you were typing it into your .py file or interpreter.

When debugging the associated exec () code it is useful to log whatever you are about to "exec" when you run into problems, if you did, you would easily notice that E0 is not assigned to the "zbc" string, but not the non-existent zbc object.

Also, this code example is really weird. There are several legitimate uses for parsing strings into instance variables or objects in other namespaces, especially when you are coding a highly dynamic class that needs to do sensible things with messy input, or needs to set up a bunch of instance variables from a dict or string. But without context, the code in your question looks like you are avoiding or not understanding how to use list () and dict () objects.

I'd suggest talking a little about what you're trying to achieve the next time you ask a question around something as quirky as this. This will give people a good opportunity to come up with a better solution, or if you're approaching a particular problem in a perfectly reasonable way, don't let a bunch of answers tell you that you are doing something completely wrong.

0


source







All Articles