Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'advance.': 0.15; 'result.': 0.15; '2],': 0.16; '3],': 0.16; '[2,': 0.16; 'iterator,': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'row': 0.16; 'subject:array': 0.16; 'wrote:': 0.17; 'jan': 0.18; '>>>': 0.18; 'equivalent': 0.20; 'import': 0.21; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'skip:[ 10': 0.26; 'regular': 0.27; 'header:X -Complaints-To:1': 0.28; '>>>>': 0.29; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'replaced': 0.35; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'first': 0.61; '2013': 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: numpy array operation Date: Tue, 29 Jan 2013 15:05:09 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359489934 news.xs4all.nl 6868 [2001:888:2000:d::a6]:58784 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37897 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