Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43858 > unrolled thread

Strange behavior for a 2D list

Started by"Robrecht W. Uyttenhove" <ruyttenhove@gmail.com>
First post2013-04-18 14:35 -0600
Last post2013-04-18 21:13 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Strange behavior for a 2D list "Robrecht W. Uyttenhove" <ruyttenhove@gmail.com> - 2013-04-18 14:35 -0600
    Re: Strange behavior for a 2D list John Gordon <gordon@panix.com> - 2013-04-18 21:13 +0000

#43858 — Strange behavior for a 2D list

From"Robrecht W. Uyttenhove" <ruyttenhove@gmail.com>
Date2013-04-18 14:35 -0600
SubjectStrange behavior for a 2D list
Message-ID<mailman.793.1366317327.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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?

Thanks,

Rob

PS: I used Python 2.7.3

[toc] | [next] | [standalone]


#43860

FromJohn Gordon <gordon@panix.com>
Date2013-04-18 21:13 +0000
Message-ID<kkpnlu$qbd$1@reader1.panix.com>
In reply to#43858
In <mailman.793.1366317327.3114.python-list@python.org> "Robrecht W. Uyttenhove" <ruyttenhove@gmail.com> writes:

> 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?

y is just a list.  It happens to be a list of lists, but that doesn't make
it a "2D" list.  It's an important distinction.

y[1:5:2] is the contents of y, starting at the second element and selecting
every second element after that:

    [[7, 8, 9, 10, 11, 12, 13], [21, 22, 23, 24, 25, 26, 27]]

y[1:5:2][::3] is the contents of y[1:5:2], starting at the first element and
selecting every third element after that (and there are only two elements,
so it stops after the first one):

    [[7, 8, 9, 10, 11, 12, 13]]

Why were you expecting the other result?

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web