Path: csiph.com!usenet.pasdenom.info!aioe.org!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python,': 0.02; '16,': 0.03; 'element': 0.07; '22,': 0.09; 'explanation': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; '2.7.3': 0.16; '23,': 0.16; '24,': 0.16; '32,': 0.16; '33,': 0.16; 'numpy': 0.16; 'obviously,': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'thu,': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; '31,': 0.24; 'example.': 0.24; 'phd': 0.24; 'skip:c 70': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; '15,': 0.26; 'this:': 0.26; 'asking': 0.27; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'url:mailman': 0.30; '25,': 0.31; 'explained': 0.31; 'vertical': 0.31; 'writes:': 0.31; '\xce\xbc\xce\xbf\xcf\x85': 0.31; 'url:python': 0.33; 'alone': 0.33; 'computing': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'possible': 0.36; 'url:org': 0.36; 'list.': 0.37; 'performance': 0.37; 'expected': 0.38; 'skip:& 10': 0.38; 'thank': 0.38; '8bit%:86': 0.38; 'pm,': 0.38; 'rather': 0.38; '12,': 0.39; 'skip:- 60': 0.39; 'university': 0.39; 'url:mail': 0.40; 'skip:u 10': 0.60; 'simple,': 0.60; 'break': 0.61; 'staff': 0.61; 'first': 0.61; 'high': 0.63; 'kind': 0.63; 'such': 0.63; '30,': 0.65; '26,': 0.68; 'skip:w 40': 0.68; '8bit%:100': 0.72; 'replies.': 0.84; '8bit%:90': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=lQSi/Oj/wVEzER6W0mzuUwqjNyzEikNEAa4nuFoq2SQ=; b=OJDw0xY6KGXH7Lc1v1ycUvyrMPMxIJgshqEKwXfOMWd407y82kKC4WbHZJDAyEBQ+5 L3yCLhZ9GsV5EUnbNVseTiyUcqx0flgZhDMchxrFlZJSnfjboqkXEKbaF6YJjeuqqptB 4hd2htnS5FMt7c60nPBOO2xLzGqSt9AWcfWJ5yLQTJ7fHRBDseRmApQSrCFtn3b4huyu EbischbsEF2uqEY7jwxXxh16vTOATwhTo6DPAPk2y3sj/HkM1mQcs36RIjdJBAbKoL0T 0/l7IhRZwexnHBSjFOIYBSPsueIBI8UxhnQDGiyPz/xk45HfrjLlKAQT8+g/OvpztnTQ 8NDA== MIME-Version: 1.0 X-Received: by 10.50.237.71 with SMTP id va7mr14205528igc.88.1366320790560; Thu, 18 Apr 2013 14:33:10 -0700 (PDT) In-Reply-To: References: Date: Thu, 18 Apr 2013 15:33:10 -0600 Subject: Re: Strange behavior for a 2D list From: "Wim R. Cardoen" To: Wolfgang Maier Content-Type: multipart/alternative; boundary=f46d0447a2435ee9a204daa95902 Cc: python-list@python.org 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: 156 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366320798 news.xs4all.nl 2215 [2001:888:2000:d::a6]:60156 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43866 --f46d0447a2435ee9a204daa95902 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thank you all for your replies. I had the matrix concept in mind such as explained in the numpy example. Rob On Thu, Apr 18, 2013 at 3:19 PM, Wolfgang Maier < wolfgang.maier@biologie.uni-freiburg.de> wrote: > Robrecht W. Uyttenhove gmail.com> writes: > > > > > Hello, > > I tried out the following > code:y=3D[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? > > > > Thanks, > > Rob > > PS: I used Python 2.7.3 > > > > The explanation is rather simple, just break up your complex slicing into > its parts: > y[1:5:2] =3D> [[7, 8, 9, 10, 11, 12, 13],[21, 22, 23, 24, 25, 26, 27]] > and [::3] is asking for the first,4th,7th,... element from this list. > Obviously, only the first one's existing, so [7, 8, 9, 10, 11, 12, 13] > > What you expected is kind of vertical slicing through the rows. I don't > think you can achieve this with slicing alone in standard Python, but it'= s > possible with numpy arrays. > In Python you will have to combine slicing with a comprehension, like thi= s: > [x[::3] for x in y[1:5:2]] > > Best, > Wolfgang > > -- > http://mail.python.org/mailman/listinfo/python-list > --=20 --------------------------------------------------------------- Wim R. Cardoen, PhD Staff Scientist, Center for High Performance Computing University of Utah (801)971-4184 *=CE=BC=E1=BD=B4 =CE=BC=CE=BF=CF=85 =CF=84=CE=BF=E1=BD=BA=CF=82 =CE=BA=CF= =8D=CE=BA=CE=BB=CE=BF=CF=85=CF=82 =CF=84=CE=AC=CF=81=CE=B1=CF=84=CF=84=CE= =B5! (=E1=BC=88=CF=81=CF=87=CE=B9=CE=BC=CE=AE=CE=B4=CE=B7=CF=82)* --------------------------------------------------------------- --f46d0447a2435ee9a204daa95902 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Thank you all for your replies.
I = had the matrix concept in mind such as
explained in the numpy exa= mple.

Rob



On Thu, Apr 18, 2013 at 3:19 PM, Wolfgan= g Maier <wolfgang.maier@biologie.uni-freiburg.de= > wrote:
Robr= echt W. Uyttenhove <ruyttenhove <at> gmail.com> writes:

>
> Hello,
> I tried out the following
code:y=3D[range(0,7),range(7,14),range(14,21),range(21,28),range(28,35)] > >>> y[[0, 1, 2, 3, 4, 5, 6], =C2=A0[7, 8, 9, 10, 11, 12, 13],=
> =C2=A0 =C2=A0 =C2=A0 [14, 15, 16, 17, 18, 19, 20], =C2=A0[21, 22, 23, = 24, 25, 26, 27], =C2=A0[28, >
=C2=A0 =C2=A0 =C2=A0 =C2=A029, 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],
> =C2=A0[21, 24, 27]]
>
> Any ideas?
>
> Thanks,
> Rob
> PS: I used Python 2.7.3
>

The explanation is rather simple, just break up your complex sl= icing into
its parts:
y[1:5:2] =3D> [[7, 8, 9, 10, 11, 12, 13],[21, 22, 23, 24, 25, 26, 27]] and [::3] is asking for the first,4th,7th,... element from this list.
Obviously, only the first one's existing, so [7, 8, 9, 10, 11, 12, 13]<= br>
What you expected is kind of vertical slicing through the rows. I don't=
think you can achieve this with slicing alone in standard Python, but it= 9;s
possible with numpy arrays.
In Python you will have to combine slicing with a comprehension, like this:=
[x[::3] for x in y[1:5:2]]

Best,
Wolfgang

--
http://mail.python.org/mailman/listinfo/python-list



--
-------------------------= --------------------------------------
Wim R. Cardoen, PhD
Staff Scie= ntist,
Center for High Performance Computing
University of Utah
(801)971-4184

=CE=BC=E1=BD=B4 =CE=BC=CE=BF=CF=85 =CF=84=CE=BF=E1= =BD=BA=CF=82 =CE=BA=CF=8D=CE=BA=CE=BB=CE=BF=CF=85=CF=82 =CF=84=CE=AC=CF=81= =CE=B1=CF=84=CF=84=CE=B5! (=E1= =BC=88=CF=81=CF=87=CE=B9=CE=BC=CE=AE=CE=B4=CE=B7=CF=82)
-----------------------------= ----------------------------------
--f46d0447a2435ee9a204daa95902--