Pythonic way to iterate through multidimensional space?
Wojciech Giel
wojtekgiel at gmail.com
Wed Aug 6 04:04:16 EDT 2014
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!
More information about the Python-list
mailing list