Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'skip:[ 20': 0.04; '21,': 0.07; 'column': 0.07; '22,': 0.09; '34,': 0.09; '40,': 0.09; 'function,': 0.09; 'works.': 0.09; 'def': 0.12; 'jan': 0.12; '23,': 0.16; '24],': 0.16; '32,': 0.16; '33,': 0.16; '39,': 0.16; 'be:': 0.16; 'index.': 0.16; 'numpy': 0.16; 'range(0,': 0.16; 'res': 0.16; 'wrote:': 0.18; 'thu,': 0.19; '>>>': 0.22; '15,': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'leave': 0.29; 'am,': 0.29; 'wonder': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'shorter': 0.36; 'subject:?': 0.36; 'skip:- 20': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; '2nd': 0.60; 'subject:Can': 0.60; 'skip:t 30': 0.61; 'skip:n 10': 0.64; 'places': 0.64; '20,': 0.68; '26,': 0.68; '1st': 0.74; 'subject:this': 0.83; '(ie': 0.84; '2015': 0.84; 'is)': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=3vSClBU62mzMi6dxgEc9Tvc2kcgvl3fW7RW3LbhzkHY=; b=Wm9jZdv2vsL8WSm5+gxH44Ap0D69cr2HC/5Wio0GScjprXlRt0Q59r2NLhvty0pxaj FIKOGO7+jhPm+0Gioksm7oUFuPHFAIKzyMonEIeabtQy5sf8+XsjvjAb2P/1SG0KR1Hs At/t1qva3hA0dM7ELB/3T1hek4DGE1OXS/ItXjThGJwjuI8+A7W/pJ/CmSFpyCUdErit erixTq/7JWRnzWyWwawhOsyb3FgtmYILiq/HEQECKAFW8m8ik8dv+Z6HiK+Li8Qz+qek pZpeQW+RijJbSlhadMiQZhf3zaNrZ/8sTgl292leOgjnwotg5r4Yhgjwo6Ea/zWyrvlx KX+g== X-Received: by 10.70.88.164 with SMTP id bh4mr17619177pdb.96.1420745306946; Thu, 08 Jan 2015 11:28:26 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <71ecb9ec-5d8c-4503-a75b-1d4aeca79b08@googlegroups.com> References: <71ecb9ec-5d8c-4503-a75b-1d4aeca79b08@googlegroups.com> From: Ian Kelly Date: Thu, 8 Jan 2015 12:27:46 -0700 Subject: Re: Can numpy do better than this? To: Python Content-Type: text/plain; charset=UTF-8 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420745315 news.xs4all.nl 2958 [2001:888:2000:d::a6]:42901 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83365 On Thu, Jan 8, 2015 at 10:56 AM, Rustom Mody wrote: > Given a matrix I want to shift the 1st column 0 (ie leave as is) > 2nd by one place, 3rd by 2 places etc. > > This code works. > But I wonder if numpy can do it shorter and simpler. > > --------------------- > def transpose(mat): > return([[l[i] for l in mat]for i in range(0,len(mat[0]))]) > def rotate(mat): > return([mat[i][i:]+mat[i][:i] for i in range(0, len(mat))]) > def shiftcols(mat): > return ( transpose(rotate(transpose(mat)))) Without using numpy, your transpose function could be: def transpose(mat): return list(zip(*mat)) numpy provides the roll function, but it doesn't allow for a varying shift per index. I don't see a way to do it other than to roll each column separately: >>> mat = np.array([[1,2,3,4,5,6], ... [7,8,9,10,11,12], ... [13,14,15,16,17,18], ... [19,20,21,22,23,24], ... [25,26,27,28,29,30], ... [31,32,33,34,35,36], ... [37,38,39,40,41,42]]) >>> res = np.empty_like(mat) >>> for i in range(mat.shape[1]): ... res[:,i] = np.roll(mat[:,i], -i, 0) ... >>> res array([[ 1, 8, 15, 22, 29, 36], [ 7, 14, 21, 28, 35, 42], [13, 20, 27, 34, 41, 6], [19, 26, 33, 40, 5, 12], [25, 32, 39, 4, 11, 18], [31, 38, 3, 10, 17, 24], [37, 2, 9, 16, 23, 30]])