如何对元组的一维 ndarray 进行切片

发布于 2025-01-20 07:15:56 字数 558 浏览 3 评论 0 原文

给出一个一维 numpy ndarray,如下图所示: 输入图片此处描述

形状为:(9,) 类类型为:

如何对该数据结构进行切片以获得第三列和第四列(宽松地使用术语“列”),如图所示:

在此处输入图像描述

请注意,我可以使用转换为 pandas 数据帧来处理此问题,但我的核心问题是我如何对其进行切片使用 numpy 或任何内置的 python 切片函数?

Give a 1-dimensional numpy ndarray as seen in the image below:
enter image description here

The shape is: (9,)
The class type is: <class 'numpy.ndarray'>

How do I slice this data structure so as to get the 3rd and 4th columns (using the term columns loosely) as seen in this image:

enter image description here

Note that I could have used a conversion to pandas dataframe to handle this but my core question is how can I slice this using numpy or any inbuilt python slicing function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

差↓一点笑了 2025-01-27 07:15:56

我建议将元组数组转换为数组数组,然后使用 numpy 切片:

arr = np.array([np.array(list(x))  for x in arr])
arr[:,2:4]

I suggest to convert array of tuples to array of arrays then use numpy slice:

arr = np.array([np.array(list(x))  for x in arr])
arr[:,2:4]
青衫儰鉨ミ守葔 2025-01-27 07:15:56

你可以尝试这个

sliced = [l[2:4] for l in my_list]

you can try this

sliced = [l[2:4] for l in my_list]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文