0

I'm very beginner at python and gurobipy library, and I want to generate bunch of individual variables with indices, like x1, x2, ... ,x100... Is there any faster and easier way to generate them? I tried "for" structure in various form but shell keeps returning syntax error message... :( HELP PLEASE!

Jung Jihye
  • 21
  • 2

2 Answers2

1

If you just want a group of values that can be indexed, use a list (indexes are 0 start):

mylist = ["some item", 23, "other item", 99]
twentythree = mylist[1]

You can add items to the list with .append(<item>):

mylist.append("new item")

You can make a list of 100 items with range(100), but the resulting list will contain values 0 to 99.

monkut
  • 42,176
  • 24
  • 124
  • 155
0

Can't say that I like doing it this way - but whenever you have to do such tedious work manually...

for i in range(1,101):
    globals()["x{}".format(i)] = "test"

print x53 # prints test
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129