Dear all,

I am trying to create x attributes in a class, to the effect of:

class classic:

def __init__(self,text):
r=range(text.count('TI -'))
for l in r:
self.l=None

Unfortunately I can't iterate through for some reason. I only get self.l, not, for instance self.1 to self.107

How do I do this? Is this just not done? Basically I'm taking about 100 sets of abstracts from a text file and want to parse by occurence then further into title, id number etc.:

so I'd get attributes in the end like:

self.1.abstract
self.1.title
self.3.id

and so forth.

Little help?

You would generally use a dictionary for this (and please do not use "l", "i", or "o" for single digit variables--they look too much like numbers). The dictionary key would point to a list ["abstract", "title", "ID"]

class classic:

   def __init__(self,text):
      text ="TI -xxTI -yy TI -z"
      self.class_dic = {}
      for  r in range(text.count('TI -')):
         self.class_dic[r] = ["empty", "empty", "empty"]

      ##   change the "ID" (element 2) for key=1
      self.class_dic[1][2] = "ID test"
      print self.class_dic

If you really want to use a class for abstract, title, and ID, then the dictioary key can point to a class instance instead of a list.

In fact, I had this lying around. Pretty basic but should give you an idea.

class Class1 :
   def __init__ (self) :
      self.field1 = "***"
      self.field2 = 0
      self.field3 = "EMPTY"

#==========================================================================
ctr = 0
class_dic = {}

C = Class1()
C.field1 = "Test1"
C.field2 = 1
C.field3 = "Test1A"
class_dic[ctr] = C
ctr += 1

C = Class1()
##C.field1 = "Test2"
C.field2 = 2
C.field3 = "Test2A"
class_dic[ctr] = C
ctr += 1

C = Class1()
C.field1 = "Test3"
C.field2 = 3
##C.field3 = "Test3A"
class_dic[ctr] = C

class_dic[1].field3 = "ID test"

for D in class_dic :
   print D, class_dic[D].field1, class_dic[D].field2, class_dic[D].field3
This question has already been answered. Start a new discussion instead.