Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '16,': 0.03; '22,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'slices': 0.09; '23,': 0.16; '24,': 0.16; '32,': 0.16; '33,': 0.16; 'numpy': 0.16; 'operates': 0.16; 'operation.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; '15,': 0.26; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'list:': 0.30; 'subject:list': 0.30; '25,': 0.31; '>>>>': 0.31; 'really': 0.36; 'two': 0.37; 'list': 0.37; 'expected': 0.38; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'happen': 0.63; 'skip:n 10': 0.64; '(that': 0.65; '30,': 0.65; '26,': 0.68; 'containing': 0.69; 'together,': 0.84; 'steps.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Strange behavior for a 2D list Date: Thu, 18 Apr 2013 23:12:52 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084ad1c.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366319582 news.xs4all.nl 2310 [2001:888:2000:d::a6]:45450 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43859 Robrecht W. Uyttenhove wrote: > Hello, > > I tried out the following code: > y=[range(0,7),range(7,14),range(14,21),range(21,28),range(28,35)] >>>> y > [[0, 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]] > >>>> y[1:5:2][::3] > [[7, 8, 9, 10, 11, 12, 13]] > > I expected the 2D list: > [[ 7, 10, 13], > [21, 24, 27]] > > Any ideas? It is not really a 2D list; rather a list of lists. You cannot see the two slices together, the slicing happens in two separate steps. y[1:5:2] is a list containing two items (that happen to be lists) >>> x = y[1:5:2] >>> x [[7, 8, 9, 10, 11, 12, 13], [21, 22, 23, 24, 25, 26, 27]] x[::3] then operates on the outer list >>> x[::3] [[7, 8, 9, 10, 11, 12, 13]] By the way, numpy offers the behaviour you expected: >>> import numpy >>> a = numpy.array(y) >>> a array([[ 0, 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]]) >>> a[1:5:2,::3] array([[ 7, 10, 13], [21, 24, 27]]) Note that both slices are passed in the same a[...] operation.