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

Popular posts from this blog

How can I add a seperate action listener to all 14 of my buttons at once that can determine player turn?(java) -

php - Sorting an multidimension array using usort fails -

Simple file compression in C -