Strange behavior for a 2D list

  • Thread starter Robrecht W. Uyttenhove
  • Start date
R

Robrecht W. Uyttenhove

Hello,

I tried out the following code:
y=[range(0,7),range(7,14),range(14,21),range(21,28),range(28,35)][[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]]
[[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
 
J

John Gordon

In said:
I tried out the following code:
y=[range(0,7),range(7,14),range(14,21),range(21,28),range(28,35)][[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]]
[[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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top