[Numpy-discussion] Simple way to shift array elements

Gökhan Sever gokhansever@gmail....
Sat Apr 10 19:49:38 CDT 2010


On Sat, Apr 10, 2010 at 7:31 PM, Charles R Harris <charlesr.harris@gmail.com
> wrote:

>
>
> On Sat, Apr 10, 2010 at 6:17 PM, Gökhan Sever <gokhansever@gmail.com>wrote:
>
>> Hello,
>>
>> Is there a simpler way to get "c" from "a"
>>
>> I[1]: a = np.arange(10)
>>
>> I[2]: b = a[3:]
>>
>> I[3]: b
>> O[3]: array([3, 4, 5, 6, 7, 8, 9])
>>
>> I[4]: c = np.insert(b, [7]*3, 0)
>> O[5]: array([3, 4, 5, 6, 7, 8, 9, 0, 0, 0])
>>
>> a and c have to be same in length and the left shift must be balanced with
>> equal number of 0's
>>
>>
> Maybe something like
>
> In [1]: a = np.arange(10)
>
> In [2]: b = zeros_like(a)
>
> In [3]: b[:-3] = a[3:]
>
> In [4]: b
> Out[4]: array([3, 4, 5, 6, 7, 8, 9, 0, 0, 0])
>
> Chuck
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
Thanks,

Your ways are more obvious than my first approach. With a bit more playing I
get a one-liner:

c=np.append(a[3:], [0]*3)

-- 
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100410/28086283/attachment-0001.html 


More information about the NumPy-Discussion mailing list