2

I'm trying to define k variables looking like x1, x2, ...xn, where each is a list (each list has 5 elements). meaning:

something like this:

     for i in [1..100]:
              "x" + str(i) = [ i + 2, i + 3, i + 3, i + 2, i + 4] 
              print ""x" + str(i)", 'x' + str(i) 

    ideal  output:
              x1 = [3,4,4,3,5]
              x2 = ...
              x3
              x4 
               .
               .
               .
              x100 = 

I am getting 'invalid syntax' messages. I'm open to anything that can get me over this fairly simple hurdle.

7
  • 5
    You definitely don't want to create a hundred variables, each called x1, x2, x3... Wouldn't you rather create a list that contains a hundred lists? That way you could access them as x[0], x[1], x[2]... Commented Jul 21, 2013 at 0:08
  • That sounds like a better plan. So I'd have one giant list? But, eventually I will be doing arithmetic on the lists. Can I do something like x[0] - x[1]? Commented Jul 21, 2013 at 0:16
  • @NilesBernoulli I think we'll be better able to help you if you give an idea of what problem you're trying to solve. The code you have right now looks like you're trying to create 100 different lists, which seems a bit silly. Commented Jul 21, 2013 at 0:17
  • By arithmetic, you mean you want to subtract all five of the items in one list from all five of the items in another? That's sort of a different question than this one: perhaps you should ask it separately (after all, you couldn't do x1 - x2 in your approach). Or do you mean you want to do x[0][0] - x[1][0] or something like that? Commented Jul 21, 2013 at 0:19
  • 1
    Python does not define tuple subtraction or exponentiation the way you want. If you're doing a lot of math, you may want to look into numpy, which provides a variety of useful mathematical operations and an n-dimensional array type. Commented Jul 21, 2013 at 0:30

2 Answers 2

3

What you want is a list of lists. With a list comprehension, that'd be

lists = [[i+2, i+3, i+3, i+2, i+4] for i in range(1, 101)]

That's equivalent to the following for loop:

lists = []
for i in range(1, 101):
    lists.append([i+2, i+3, i+3, i+2, i+4])

Instead of writing x1, you'd write lists[0]. If you wanted to do math on the elements,

lists[3][2] - lists[6][3]

would be what you'd write instead of

x4[2] - x7[3]
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this with a class or with globals or locals dict for example:

class foo: pass

setattr(foo, 'variable', 'value')

print foo.variable

another example with __dict__:

 class bar:
    def add_var(self, variable, value):
        self.__dict__[variable] = value
bar = bar()
bar.add_var("variable", "value")
print bar.variable

an example with globals:

globals()['variable'] = "value"
print variable

the same with locals:

locals()['var'] = "value"
print var

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.