# Numeric slicing, iteration prob.

Duncan Smith buzzard at urubu.freeserve.co.uk
Mon Aug 5 14:48:45 CEST 2002

```Hello,
Does anyone know of a cuter way of iterating over an index rather
than building strings and using eval()? (Example below.)  This will get more
awkward when I need to do further slicing.  eg. depending on the results
returned I might need to generate t[1,:,3], t[:,1,3] etc.

On a related note, does anyone have a feeling for the most efficient way of
checking if all the values in a returned slice are zero?  I can obviously
just iterate over the cells and break when I get a non-zero value, but I'm
hoping there's some more efficient method available in Numeric.  TIA.
Cheers.

Duncan

>>> import RandomArray
>>> t = RandomArray.randint(0, 9, [2,3,4])
>>> t
array([[[6, 0, 2, 4],
[4, 6, 6, 2],
[8, 3, 3, 3]],
[[3, 2, 2, 4],
[4, 1, 6, 1],
[1, 6, 0, 4]]])
>>> index = (1,1,3)
>>> for i in range(len(index)):
...  s = 't[' + ':,'*i + str(index[i]) + ',...]'
...  eval(s)
...
array([[3, 2, 2, 4],
[4, 1, 6, 1],
[1, 6, 0, 4]])
array([[4, 6, 6, 2],
[4, 1, 6, 1]])
array([[4, 2, 3],
[4, 1, 4]])
>>>

```