Good morning,
I've been searching through the internet a way to make a list or table with 2 indexes. Currently I've build a code for 1 index that is mass = list(range(0,value(m.n)+1))
and I fill this list with this expression mass[i] = value(m.mass[i])
.
But now I have to introduce another variable that has two indexes as the following x[i,j] = value(m.x[i,j])
. I've tried with x = []*value(m.ph)
. But what I want is to get something like x = [value(m.n)]*value(m.ph)
in order to state that I have a table like. values (m.n) as rows and phases (m.ph) as columns.
The goal of doing so is graphing those variables with this function plt.plot(time,x)
. Idk if it will be possible to do this kind of list, table or matrix as you want to call it, but it will be very useful to know it because I got kinda stuck because of it.
My code is as following:
n = Param(initialize = 10, within = Integers)
N = Set(initialize = range(0,value(m.n)+1))
#What I got
x1 = [0,1,2,3,4,5,6,7,8,9,10]
x = list(range(0,value(m.n)+1))
for i in m.N:
x[i] = x1[i]
#What I try to do
ph = Param(initialize = 2, within = Integers)
Ph = Set(initialize = range(0,value(m.n)+1))
x2 = [[0,0],[1,1],[2,2],[3,3],[4,4]]
x = []*value(m.ph)
for i in m.N:
for j in m.Ph:
x[i,j] = x2[i,j]
The error is as following:
File "C:/Users/Manuel/Desktop/StackExample.py", line 38, in <module>
x[i,j] = x2[i,j]
TypeError: list indices must be integers or slices, not tuple
Thank you so much.
EDIT: What I want is create a list with two indexes in Python making those indexes have the length I required being these lenght defined by m.n = Param(initialize = 32, within = Integers)
and m.ph = Param(initialize = 2, within = Integers)
.