Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37855 > unrolled thread

numpy array operation

Started by"C. Ng" <ngcbmy@gmail.com>
First post2013-01-29 00:41 -0800
Last post2013-01-29 15:05 -0500
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  numpy array operation "C. Ng" <ngcbmy@gmail.com> - 2013-01-29 00:41 -0800
    Re: numpy array operation Peter Otten <__peter__@web.de> - 2013-01-29 10:28 +0100
    Re: numpy array operation Tim Williams <tjandacw@cox.net> - 2013-01-29 04:59 -0800
    Re: numpy array operation Alok Singhal <as8ca@virginia.edu> - 2013-01-29 18:49 +0000
      Re: numpy array operation Terry Reedy <tjreedy@udel.edu> - 2013-01-29 15:05 -0500

#37855 — numpy array operation

From"C. Ng" <ngcbmy@gmail.com>
Date2013-01-29 00:41 -0800
Subjectnumpy array operation
Message-ID<f94d7654-2b87-404b-a0b0-76f1ccf8ee6c@googlegroups.com>
Is there a numpy operation that does the following to the array?

1 2  ==>  4 3
3 4       2 1

Thanks in advance.

[toc] | [next] | [standalone]


#37856

FromPeter Otten <__peter__@web.de>
Date2013-01-29 10:28 +0100
Message-ID<mailman.1167.1359451658.2939.python-list@python.org>
In reply to#37855
C. Ng wrote:

> Is there a numpy operation that does the following to the array?
> 
> 1 2  ==>  4 3
> 3 4       2 1

How about

>>> a
array([[1, 2],
       [3, 4]])
>>> a[::-1].transpose()[::-1].transpose()
array([[4, 3],
       [2, 1]])

Or did you mean

>>> a.reshape((4,))[::-1].reshape((2,2))
array([[4, 3],
       [2, 1]])

Or even

>>> -a + 5
array([[4, 3],
       [2, 1]])

[toc] | [prev] | [next] | [standalone]


#37858

FromTim Williams <tjandacw@cox.net>
Date2013-01-29 04:59 -0800
Message-ID<3111692c-2341-4c6d-9bea-35d6167a0c94@googlegroups.com>
In reply to#37855
On Tuesday, January 29, 2013 3:41:54 AM UTC-5, C. Ng wrote:
> Is there a numpy operation that does the following to the array?
> 
> 
> 
> 1 2  ==>  4 3
> 
> 3 4       2 1
> 
> 
> 
> Thanks in advance.

>>> import numpy as np
>>> a=np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> np.fliplr(np.flipud(a))
array([[4, 3],
       [2, 1]])

[toc] | [prev] | [next] | [standalone]


#37895

FromAlok Singhal <as8ca@virginia.edu>
Date2013-01-29 18:49 +0000
Message-ID<ke95jk$v42$1@dont-email.me>
In reply to#37855
On Tue, 29 Jan 2013 00:41:54 -0800, C. Ng wrote:

> Is there a numpy operation that does the following to the array?
> 
> 1 2  ==>  4 3
> 3 4       2 1
> 
> Thanks in advance.

How about:

>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a[::-1, ::-1]
array([[4, 3],
       [2, 1]])

[toc] | [prev] | [next] | [standalone]


#37897

FromTerry Reedy <tjreedy@udel.edu>
Date2013-01-29 15:05 -0500
Message-ID<mailman.1192.1359489934.2939.python-list@python.org>
In reply to#37895
On 1/29/2013 1:49 PM, Alok Singhal wrote:
> On Tue, 29 Jan 2013 00:41:54 -0800, C. Ng wrote:
>
>> Is there a numpy operation that does the following to the array?
>>
>> 1 2  ==>  4 3
>> 3 4       2 1
>>
>> Thanks in advance.
>
> How about:
>
>>>> import numpy as np
>>>> a = np.array([[1,2],[3,4]])
>>>> a
> array([[1, 2], [3, 4]])
>>>> a[::-1, ::-1]
> array([[4, 3], [2, 1]])
>

Nice. The regular Python equivalent is

a = [[1,2],[3,4]]
print([row[::-1] for row in a[::-1]])
 >>>
[[4, 3], [2, 1]]

The second slice can be replaced with reversed(a), which returns an 
iterator, to get
[row[::-1] for row in reversed(a)]
The first slice would have to be list(reversed(a)) to get the same result.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web