Posts: 4
Threads: 1
Joined: May 2020
I want to create and assign a value to a variables in a for loop and name the variable in the loop. This works as intended. Afterwards, I would like to print the variables something like:
1 2 3 4 5 |
a = 3
source = ( 0.1 , 0.2 , 0.3 )
for n in range (a):
exec ( "var" + str (n) + " = source[n]" )
print ( 'var' + str (n))
|
However, I end up with:
var0
var1
var2
Whereas I want output corresponding to this:
1 2 3 |
print (var0)
print (var1)
print (var2)
|
How do I change the code accordingly?
Posts: 1,838
Threads: 2
Joined: Apr 2017
You really shouldn't be trying to dynamically create variables like that. For a start, it's just over-complicated and you'll find your code becomes hard to maintain. Why exactly do you want to do it, i.e. what problem are you trying to solve?
Posts: 60
Threads: 9
Joined: May 2020
Well the reason is that "var" + str(n) creates a string, not a variable.
But yeah, you need to reconsider your approach.
Posts: 8,158
Threads: 160
Joined: Sep 2016
as advised - use proper data structure - list/dict, don't create names dynamically
Posts: 4
Threads: 1
Joined: May 2020
First of all, thanks for you feedback. Since I am new to Python, there is a lot to learn:
- Despite the fact that my proposal is not really suited, what would be the necessary adjustment such that I obtain the desired result?
- I have a number of variables that may change, here "a". This number always corresponds to the number of elements in "sources". I need to create a variable that contains the value in the list "sources" and its position in the name of the variable. What would be the right way to solve this problem?
Posts: 8,158
Threads: 160
Joined: Sep 2016
as already said - use proper data structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
sources = ( 0.1 , 0.2 , 0.3 )
print (sources[ 0 ])
print (sources[ 2 ])
foo = sources[ 1 ]
print (foo)
spam = { f 'a{idx}' :num for idx, num in enumerate (sources)}
print (spam)
print (spam.get( 'a0' ))
print (spam[ 'a1' ])
print (spam.get( 'a5' , 'wrong key' ))
spam = {}
for idx, num in enumerate (sources):
spam[ f 'a{idx}' ] = num
print (spam)
print (spam.get( 'a0' ))
print (spam[ 'a1' ])
print (spam.get( 'a5' , 'wrong key' ))
|
Output: 0.1
0.3
0.2
{'a0': 0.1, 'a1': 0.2, 'a2': 0.3}
0.1
0.2
wrong key
{'a0': 0.1, 'a1': 0.2, 'a2': 0.3}
0.1
0.2
wrong key
Posts: 4
Threads: 1
Joined: May 2020
This may be the proper way to code the problem, but it just confuses me a lot.
Is it really not possible to use what I have suggested? It may appear wrong to you, but I would like to know it anyway.
Posts: 1,838
Threads: 2
Joined: Apr 2017
May-28-2020, 03:33 PM
(This post was last modified: May-28-2020, 03:39 PM by ndc85430.)
Why does it confuse you?
It would be possible to do what you wanted, but you'd have to make more use of exec (or eval , I guess). What are the problems with that? There are at least two:
1. Those functions allow any arbitrary Python expression to be entered in a string. So, if data came in from a user, that could present a security risk (in the same way that SQL injection does).
2. You'd end up having a lot of code written in the strings you pass to those functions. That's quickly going to become unreadable as you'll lose the benefits of syntax highlighting in your editor/IDE, not to mention that you'll have lots of concatenation which you won't if you're just writing Python code in the file.
Posts: 8,158
Threads: 160
Joined: Sep 2016
read http://stupidpythonideas.blogspot.com/20...reate.html
still, you will need to learn how to work with container types like list/tuple/dicts, etc. it's a fundamental, you cannot progress beyond basics without it
Posts: 4
Threads: 1
Joined: May 2020
The assignment of the values to the variables (which in fact are scalars) works just fine. I just would like to display the values of these scalars within the loop. I get it, it is wrong. Still it is what I want to do
|