Pythonic way to iterate through multidimensional space?

Gayathri J usethisid2014 at gmail.com
Wed Aug 6 08:13:43 EDT 2014


Dear Peter

Yes the f[t] or f[:,:,:] might give  a marginal increase, but then i need
to do further operations  using the indices, in which case this wouldnt help


Dear Wojciech

np.flat() works if u dont care about the indices and only the matrix/array
values matter.
but if the <i,j,k> matters, flatten wouldnt work



On Wed, Aug 6, 2014 at 1:34 PM, Wojciech Giel <wojtekgiel at gmail.com> wrote:

> You might check numpy it is really powerful tool for working with multi
> dimensional arrays:
>
> ex.
> >>> a = arange(81).reshape(3,3,3,3)
> >>> a
>
> array([[[[ 0,  1,  2],
>          [ 3,  4,  5],
>          [ 6,  7,  8]],
>
>         [[ 9, 10, 11],
>          [12, 13, 14],
>          [15, 16, 17]],
>
>         [[18, 19, 20],
>          [21, 22, 23],
>          [24, 25, 26]]],
>
>
>        [[[27, 28, 29],
>          [30, 31, 32],
>          [33, 34, 35]],
>
>         [[36, 37, 38],
>          [39, 40, 41],
>          [42, 43, 44]],
>
>         [[45, 46, 47],
>          [48, 49, 50],
>          [51, 52, 53]]],
>
>
>        [[[54, 55, 56],
>          [57, 58, 59],
>          [60, 61, 62]],
>
>         [[63, 64, 65],
>          [66, 67, 68],
>          [69, 70, 71]],
>
>         [[72, 73, 74],
>          [75, 76, 77],
>          [78, 79, 80]]]])
>
> >>> f = a.flat
> >>> for i in f:
> ...    print(i)
> 0
> 1
> 2
> ..
> 98
> 99
>
> cheers
> Wojciech
>
>
>
>
> On 05/08/14 21:06, Frank Miles wrote:
>
>> I need to evaluate a complicated function over a multidimensional space
>> as part of an optimization problem.  This is a somewhat general problem
>> in which the number of dimensions and the function being evaluated can
>> vary from problem to problem.
>>
>> I've got a working version (with loads of conditionals, and it only works
>> to #dimensions <= 10), but I'd like something simpler and clearer and
>> less hard-coded.
>>
>> I've web-searched for some plausible method, but haven't found anything
>> "nice".  Any recommendations where I should look, or what technique should
>> be used?
>>
>> TIA!
>>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140806/40f191c5/attachment.html>


More information about the Python-list mailing list