0

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
  • stackoverflow.com/questions/1761419/… Commented Sep 11, 2012 at 1:33
  • 1
    @AshwiniChaudhary: I think the OP is already using numpy. "output operand requires a reduction [etc.]" is a numpy error message. Commented Sep 11, 2012 at 1:35

2 Answers 2

1

You seem to have several issues with python:

  1. 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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.