6

I looked around and could not find anything this specific, so here goes:

I have a list of lists:

S = [[3,4],[6,7],[10,12]]

and I would like to add the 0th index of the ith index element and on to the end of another list:

R = [5,6,7]

Normally with a 1D list I could say:

R = R + S[i:]

and take all of the elements from the ith index on, but I want the 0th index of the ith index of a 2D S. If we started at i = 1 I would end up with:

R = [5,6,7,6,10]

Also, I don't want to use for loops I want a list slice method that will work (if there is one) because it needs to be within a certain bound.

Roklobster
  • 63
  • 1
  • 5
  • 2
    "because it needs to be within a certain bound" - what do you mean by that? It's not clear what sorts of bounds would be violated by a for loop, but not violated by slicing. – user2357112 Nov 14 '15 at 22:29
  • 3
    If you're working a lot with nested lists standing in for multidimensional arrays, consider `numpy`. – jonrsharpe Nov 14 '15 at 22:31
  • @user2357112 : I have sample code that is using the 1D method of just adding the sliced part of S[i:] to the end of R, so I assume that using a method similar to that will maintain the bounds needed. Also, as I learn some cleaner python style code I was wondering if there is a way to do it this way. – Roklobster Nov 14 '15 at 22:39
  • @jonrsharpe : This is part of an assignment which can only use built-in libraries so numpy is out, otherwise I would definitely use it. – Roklobster Nov 14 '15 at 22:41
  • 1
    @Roklobster if this is an assignment, shouldn't you be doing it yourself? – jonrsharpe Nov 14 '15 at 22:58
  • @jonrsharpe : This is actually such a small portion of the assignment, and when I came across either using slices or using a for loop, I was wondering whether or not I could do it. If I were lazy I could have easily just kept my answer as I had it with: s = [S[i][0] for i in range(len(S))] R = R + s But I'm curious if there is a cleaner way using a slice. – Roklobster Nov 14 '15 at 23:03

2 Answers2

5

You can use zip to transpose the matrix:

>>> S
[[3, 4], [6, 7], [10, 12]]
>>> zip(*S)
[(3, 6, 10), (4, 7, 12)]

Then slice the transposition:

>>> j=0
>>> i=1
>>> zip(*S)[j][i:]
(6, 10)

A tuple is iterable, so concatenation will work with a list:

>>> R = [5,6,7]
>>> R+=zip(*S)[j][i:]
>>> R
[5, 6, 7, 6, 10]
dawg
  • 98,345
  • 23
  • 131
  • 206
1

As @jonrsharpe mentioned, numpy will do the trick for you:

import numpy as np
# Create two arrays
S = np.asarray([[3,4],[6,7],[10,12]])
R = np.asarray([5, 6, 7])
# Slice the S array
i = 1
sliced_S = S[i:, 0]
# Concatenate the arrays
R = np.concatenate((R, sliced_S))

Have a look at numpy's impressive documentation and indexing in particular.

Till Hoffmann
  • 9,479
  • 6
  • 46
  • 64
  • As I told @jonrsharpe, I can only use built in libraries because it is being tested on a cs department cluster, and I can't be sure numpy is usable. – Roklobster Nov 14 '15 at 22:48
  • In case of homework, we don't tend to provide solutions but rather tips: Have a look at the `zip` function and http://stackoverflow.com/a/17037588/1150961 in particular. – Till Hoffmann Nov 14 '15 at 22:51
  • Till, I'm not really looking for a homework solution as much as a best practice, but this is a solid way of doing it. – Roklobster Nov 15 '15 at 00:03