Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > numpy array operation

Reply
Thread Tools

numpy array operation

 
 
C. Ng
Guest
Posts: n/a
 
      01-29-2013
Is there a numpy operation that does the following to the array?

1 2 ==> 4 3
3 4 2 1

Thanks in advance.


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-29-2013
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]])


 
Reply With Quote
 
 
 
 
Tim Williams
Guest
Posts: n/a
 
      01-29-2013
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]])

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      01-29-2013
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing numpy over an older numpy Tom Kacvinsky Python 1 06-15-2012 07:15 PM
A More Concise Description of Numpy than the Guide to Numpy? W. eWatson Python 2 11-23-2009 08:58 PM
NumPy Question - numpy.put in multi-dimensional array Bryan.Fodness@gmail.com Python 2 11-13-2007 10:36 PM
numpy migration (also posted to numpy-discussion) Duncan Smith Python 3 04-25-2007 01:36 AM
Trouble with numpy-0.9.4 and numpy-0.9.5 drife Python 1 03-01-2006 05:59 PM



Advertisments