[英]Numpy python array slicing
I have an array as shown below,我有一个如下所示的数组,
[[240.66666667 171.22222222 158.33333333]
[218.66666667 134.77777778 143.33333333]
[197.33333333 118.55555556 128.44444444]
[195.22222222 119.33333333 126.11111111]
[196.77777778 118.55555556 123.77777778]
[183.11111111 111.88888889 118.88888889]
[173.77777778 106.77777778 114.44444444]]
I want to slice the first and third columns for all the rows and wanna get this output,我想对所有行的第一列和第三列进行切片,并想得到这个 output,
[[240.66666667 158.33333333]
[218.66666667 143.33333333]
[197.33333333 128.44444444]
[195.22222222 126.11111111]
[196.77777778 123.77777778]
[183.11111111 118.88888889]
[173.77777778 114.44444444]]
Does anyone have any ideas?有没有人有任何想法?
Output screenshot: Output 截图:
You can just give the columns you want like,你可以只给你想要的列,
>>> data
array([[240.66666667, 171.22222222, 158.33333333],
[218.66666667, 134.77777778, 143.33333333],
[197.33333333, 118.55555556, 128.44444444],
[195.22222222, 119.33333333, 126.11111111],
[196.77777778, 118.55555556, 123.77777778],
[183.11111111, 111.88888889, 118.88888889],
[173.77777778, 106.77777778, 114.44444444]])
>>> data[:, [0,2]]
array([[240.66666667, 158.33333333],
[218.66666667, 143.33333333],
[197.33333333, 128.44444444],
[195.22222222, 126.11111111],
[196.77777778, 123.77777778],
[183.11111111, 118.88888889],
[173.77777778, 114.44444444]])
>>>
You can do that with numpy.delete
function easily as shown below:您可以使用numpy.delete
function 轻松做到这一点,如下所示:
a = np.array([[240.66666667, 171.22222222, 158.33333333],
[218.66666667, 134.77777778, 143.33333333],
[197.33333333, 118.55555556, 128.44444444],
[195.22222222, 119.33333333, 126.11111111],
[196.77777778, 118.55555556, 123.77777778],
[183.11111111, 111.88888889, 118.88888889],
[173.77777778, 106.77777778, 114.44444444]])
a = np.delete(a,1,axis=1)
With that piece of code, you can get the output you want.有了那段代码,你就可以得到你想要的output。
Output:
[[240.66666667 158.33333333]
[218.66666667 143.33333333]
[197.33333333 128.44444444]
[195.22222222 126.11111111]
[196.77777778 123.77777778]
[183.11111111 118.88888889]
[173.77777778 114.44444444]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.