I am trying to store as a variable in a python script, mxn arrays using nested loops as follows:
A=[ ]
for j in ListA:
for x in ListB:
values = some.function(label_fname, stc_fname)
A(j)=values(x)
for each x, values is an mxn matrix with m~=n.
When I index values here by values[x] or values(x) I get:
output operand requires a reduction, but reduction is not enabled OR can't assign to function call.
What I would like to due is append values(x) matrices and store in A(j). Honestly, I can't say this in English, but in matlab lingo I am trying to create a cell array, where A{j} is an mxn array.
Thanks in advance.
2 Answers 2
You seem to have several issues with python:
When indexing into a list, use
[and]; not(and). Also, the first element of a list is at index 0. This means that if you have a list `L = ['a', 'b', 'c', 'd'],- The index of 'a' in L is 0
- The index of 'b' in L is 1
- The index of 'c' in L is 2
- The index of 'd' in L is 3
From what I understand from your explanation, I would suggest the following code. See if it works for you:
A = []
for sub_list in ListA:
temp = []
for x in ListB:
values = some.function(label_fname, stc_fname)
temp.append(values)
A.append(temp)
I'm really not very sure what you are asking for, but hopefully, this is a good start. Hope it helps
Comments
You might be trying / expecting to create a dict() with j as the the key.
Or, for multidimensional arrays, numpy is very useful
See the dict() docs: http://docs.python.org/library/stdtypes.html#dict
Note:
> A(j) # this calls function A
> A[j] # this returns the list item 'j'
> A[j] = foo # this sets list item 'j' = foo
numpy. "output operand requires a reduction [etc.]" is anumpyerror message.