[Numpy-discussion] Index array confusion...

Todd Miller jmiller at stsci.edu
Mon Dec 16 07:01:01 CST 2002


Magnus Lie Hetland wrote:

>Magnus Lie Hetland <magnus at hetland.org>:
>[snip]
>  
>
>>This seems like a bug to me. The assignment ought to swap the tails of
>>the sequences, as is the case with lists, but with numeric arrays,
>>some weird form of overwriting occurs. I guess this may be an
>>optimization (i.e. to avoid making copies), but it is rather
>>confusing. What do you think?
>>    
>>
>
>Even stranger:
>
>  
>
>>>>a = zeros([5])
>>>>b = ones([5])
>>>>p = 2
>>>>tail_a = a[p:]
>>>>tail_b = b[p:]
>>>>a[p:] = tail_b
>>>>b[p:] = tail_a
>>>>a, b
>>>>        
>>>>
>(array([0, 0, 1, 1, 1]), array([1, 1, 1, 1, 1]))
>
>I suppose this is due to sharing of slices somehow? I.e:
>  
>
This looks to me like the same problem,  just performing the effects of 
the tuple copy one step at a time.  The key is that the "tails" are 
views and not copies.

>  
>
>>>>a = ones([5])
>>>>b = a[:]
>>>>b[2] = 0
>>>>a
>>>>        
>>>>
>array([1, 1, 0, 1, 1])
>
>I have to use the copy method here, I guess?
>  
>
Yes.   array[ slice ] --> a view of a subarray.

>  
>
>>>Todd
>>>      
>>>
>
>  
>







More information about the Numpy-discussion mailing list