zero-copy array slicing
Alex Martelli
aleax at aleax.it
Tue Nov 19 08:11:05 EST 2002
Trevor wrote:
> Is a zero-copy array slicing operation possible? Or would having two
> arrays share the same elements cause too many problems? I'm thinking
Perhaps both; in any case, that's what the justly popular Numeric
package does:
> something like:
>
>>>> a1 = array('b', [0, 1, 2, 3, 4, 5])
>>>> a2 = a1.subsequence(slice(1, 4))
>>>> a2
> array('b', [1, 2, 3])
>>>> a2[0] = 100
>>>> a1
> array('b', [0, 100, 2, 3, 4, 5])
>>> import Numeric
>>> a1=Numeric.arange(5)
>>> a1
array([0, 1, 2, 3, 4])
>>> a2 = a1[1:4]
>>> a2
array([1, 2, 3])
>>> a2[0] = 100
>>> a1
array([ 0, 100, 2, 3, 4])
>>>
So if this the behavior you need, the simplest way to get it is exactly
with the Numeric extensions -- builtin sequences such as lists and
array.array instances support the safer-but-perhaps-slower approach
of making each slice a copy, differently from what happens when you
slice a Numeric.array.
Alex
More information about the Python-list
mailing list