python - Cycling through a group of variables with similar names -
Is there a way of cycling with names similar to using loop in cycles through cycles of Python ?
Example (from nltk library): I have a group of variables named Lesson 1, Lesson 2, Lesson 3, ..., Text 9, and I have always written their names without clearly typing them All of them want to do some work, so I am exploring a way through loop through them:
for class x (1, 9): text + x.someFunction ( ) I know that this is not allowed, but is there a way to do something like that?
You can use eval:
eval (" For example, to load your values in the map: data = dict (key): key = "text" + str (x) data [key] = eval (key) or if your variable is a class object With the method someFunction : xrange (1, 9) in x: cmd = "tex t" + str (x) + ".someFunction ()" eval (cmd)
Comments
Post a Comment