I have several variables that are named as so:
self.box_1
self.box_2
self.box_3
self.square_1
self.square_2
self.square_3
self.triangle_1
self.triangle_2
self.triangle_3
I want to create a dictionary of all the 'box','squares','triangles' and a dictionary of all the 1,2 and 3 of those.
Something like:
names = {'box':[self.box_1,self.box_2,self.box_3],
'square':[self.square_1,self.square_2 ...}
numbers = {1:[self.box_1,self.square_1,self.triangle_1]
2:[self.box_2,self.square_2,... }
I've manage to make a small script to create the right string:
groups = ['box','square','triangle']
names = {}
for group in groups:
g = []
for n in xrange(1, 4):
g.append('self.%s_%s' % (group, n))
names[group] = g
numbers = {}
for n in xrange(1, 4):
g = []
for group in groups:
g.append('self.%s_%s'%(group,n))
numbers[n] = g
print "Names =",names
print "Number =",numbers
Output:
Names = {'box': ['self.box_1', 'self.box_2', 'self.box_3'], 'square': ['self.square_1', 'self.square_2', 'self.square_3'], 'triangle': ['self.triangle_1', 'self.triangle_2', 'self.triangle_3']}
Number = {1: ['self.box_1', 'self.square_1', 'self.triangle_1'], 2: ['self.box_2', 'self.square_2', 'self.triangle_2'], 3: ['self.box_3', 'self.square_3', 'self.triangle_3']}
As you can see, it's almost the same as the desired, but the names of the variables are in string format. How can I convert a string to a variable name?