Reputation: 117
I can't figure out how to store the result of a calculation in a List in SageMath.
I have combed the tutorial and help pages, but I can't seem to find this very simple thing. The Pseudocode I want is:
Loop through i
Loop through j
Loop through k
Result[i,j,k]=Calculation involving i,j, and k
The actual calculation is several lines of if statements and while statements, so I don't think it can be manipulated into the simple form I often see in Sage:
Result=[ [i,j,] for i,j in Q]
Upvotes: 0
Views: 319
Reputation: 1696
Here are some options:
sage: R = [100*i + 10*j + k for i in range(2) for j in range(2) for k in range(2)]
sage: R
[0, 1, 10, 11, 100, 101, 110, 111]
or
R = []
for i in range(2):
for j in range(2):
for k in range(2):
R.append(100*i + 10*j + k)
If you actually want something like Result[i,j,k] = ...
, i.e., with the results indexed by i, j, k, maybe you want a dictionary instead:
sage: R = {(i,j,k): 100*i + 10*j + k for i in range(2) for j in range(2) for k in range(2)}
sage: R
{(0, 0, 0): 0,
(0, 0, 1): 1,
(0, 1, 0): 10,
(0, 1, 1): 11,
(1, 0, 0): 100,
(1, 0, 1): 101,
(1, 1, 0): 110,
(1, 1, 1): 111}
sage: R[1,0,1]
101
or with the nested for
loops, initialize it with R={}
and then replace the last line in loop with R[i,j,k] = ...
Upvotes: 1