How would you copy the first element and every element of the nth column in another array?
For example, supposed you have the array below:
array{[1,2,3,4,5],
[1,2,3,4,5],
[1,2,3,4,5]}
I want to choose the first element and every 2nd element so I would have:
array{[1,3,5],
[1,3,5],
[1,3,5]}
You can use slicing against the columns
>>> a
array([[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]])
>>> a[:, ::2]
array([[1, 3, 5],
[1, 3, 5],
[1, 3, 5]])
As mentioned by @tobias_k if you want to make an actual copy of this sliced array, you can use numpy.copy
to make sure modifications don't affect the original array
>>> np.copy(a[:, ::2])
array([[1, 3, 5],
[1, 3, 5],
[1, 3, 5]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.