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!
Asked
Active
Viewed 127 times
0
-
1Use a sequence or a mapping instead of creating multiple variables. – falsetru Sep 25 '14 at 01:50
2 Answers
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