I have 2-D matrix of size n
, I want to get the entire n-1
th column values into another list. For example,
a = [[1, 2], [3, 4], [5, 6]]
a[:][0] // return [1,2]
how to get 1,3,5 for above a
2-D array into a list using slice operator
I have 2-D matrix of size n
, I want to get the entire n-1
th column values into another list. For example,
a = [[1, 2], [3, 4], [5, 6]]
a[:][0] // return [1,2]
how to get 1,3,5 for above a
2-D array into a list using slice operator
To my knowledge, the array slice operator is not suited for what you're looking for.
I would recommend python's list comprehensions.
a = [[1, 2], [3, 4], [5, 6]]
result = [x[0] for x in a]
print(result)
You can do this using the numpy
library:
import numpy
a = np.array([[1, 2], [3, 4], [5, 6]])
result = a[:, 0] # Returns a 1-D numpy array [1, 3, 5]
More advanced indexing and slicing options can be found here.