4

I'm new to python, so I don't know if this is possible. I'm trying to create a list of variable names from a list of strings so I can then assign values to the newly created variables.

I saw another similar question, but there was already a dict pays of keys and values.

Here's my code:

def handy(self):
    a = raw_input('How many hands? ')
    for i in range(int(a)):
        h = "hand" + str(i+ 1)
            self.list_of_hands.append(h)
            # would like to create variable names from self.list_of_hands
            # something like "hand1 = self.do_something(i)"
            print h
    print self.list_of_hands

Given some of the answers, i'm adding the following comment: I'm going to then assign dicts to each variable. So, can I create a dictionary of dictionaries?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • 3
    http://stackoverflow.com/questions/295058/convert-string-to-preexisting-variable-names http://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string-in-python http://stackoverflow.com/questions/6122816/how-to-convert-string-to-variable-name – avasal Aug 22 '12 at 05:21
  • @avasal At the risk of sounding very thick, I don't understand how your examples apply here. Any chance you could apply them to what I'm trying to do? many thanks. – DBWeinstein Aug 22 '12 at 05:40
  • 1
    @dwstein your trying to dynamically create variable names. The solutions mentioned in avasal's post say that you are probably better off using a dictionary with the variable names as keys, or as a list since the names are simply `hand + ` – Snakes and Coffee Aug 22 '12 at 06:58

3 Answers3

4

Why don't you just construct a dictionary using the strings as keys?

>>> class test():
    def handy(self):
        a = raw_input('How many hands? ')
        d = { "hand" + str(i + 1) : self.do_something(i) for i in range(int(a)) }

        keys = d.keys()
        keys.sort()
        for x in keys:
            print x, '=', d[x]

    def do_something(self, i):
        return "something " + str(i)

>>> test().handy()
How many hands? 4
hand1 = something 0
hand2 = something 1
hand3 = something 2
hand4 = something 3

Edit: You updated the question to ask if you can store a dictionary as a value in a dictionary. Yes, you can:

>>> d = { i : { j : str(i) + str(j) for j in range(5) } for i in range(5) }
>>> d[1][2]
'12'
>>> d[4][1]
'41'
>>> d[2]
{0: '20', 1: '21', 2: '22', 3: '23', 4: '24'}
>> d[5] = { 1 : '51' }
>> d[5][1]
'51'
verdesmarald
  • 11,646
  • 2
  • 44
  • 60
2

If you ever have multiple variables that only differ by a number at the end (hand1, hand2, etc.), you need a container.

I think a dictionary would work best:

self.hands = {}
self.hands[h] = self.do_something(i)

You can access the individual keys in the dictionary easily:

self.hands['hand1']
Blender
  • 289,723
  • 53
  • 439
  • 496
2
h = "hand" + str(i+ 1)
vars()[h] = do_something(i)

Now you can call hand1 to call do_something()

thavan
  • 2,409
  • 24
  • 32