Kambiz
Kambiz

Reputation: 715

Python iteration and multi-dimensional array structure

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.

Upvotes: 0

Views: 279

Answers (2)

inspectorG4dget
inspectorG4dget

Reputation: 113955

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

Upvotes: 1

ninMonkey
ninMonkey

Reputation: 7511

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

Upvotes: 0

Related Questions