I am trying to use variables with different names while in a for
loop.
Before I explain anything else, let me say that this is not the problem I am having. Yes, I am creating different variables based on the index of the for
loop, but I understand from that and other resources how to do that.
What I do not know how to do is how to call different variables within the for
loop. What I mean is I want something sort of like this:
# pre-defined variables
object_1 = 7
object_2 = 8
object_3 = 51
# create new dictionary
Things = {}
# populate dictionary
for i in range(1,4):
Things['thing_%d'%i] = object_i
So I know how to create separate new variables (using the dictionary Things
). What I want to do is have a way that, as seen in the last line, the program identifies the variable with index i
(object_i
) for use in defining the element in the Things
dictionary thing_i
.
Is there a way to do this without having to write a separate line of code for each variable (like shown below)?
Things['thing_1'] = object_1
Things['thing_2'] = object_2
Things['thing_3'] = object_3
UPDATED EDIT AND CLARIFICATION: The reason I cannot use a dictionary to define object_1
, object_2
, and object_3
is they are not actually defined in the Python script. I have to call them from a Salome (3D modeling program) file where they are indexed as Face_1
, Face_2
, etc. I would love to have everything in elements of an array (eliminating the need for separate variables), but in order to do that, I still need to call up the objects from Salome. I currently have a script that I am running in the Salome Python console that starts out like this so I can access the objects from the Salome study:
# Salome testing
import salome
salome.salome_init("C:\Users\Me\Study3.hdf")
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)
# need to retrieve all objects from that study -----------
go = salome.myStudy.FindObjectByPath("/Geometry")
it = salome.myStudy.NewChildIterator( go )
it.InitEx(True)
while it.More():
so = it.Value()
it.Next()
go = so.GetObject()
if go:
#print(so.GetName())
exec( "%s = go"%so.GetName() )