以numpy中的值拆分数组

编程入门 行业动态 更新时间:2024-10-06 06:52:18
本文介绍了以numpy中的值拆分数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个文件,其中包含以下格式的数据:

I have a file containing data in the format:

0.0 x1 0.1 x2 0.2 x3 0.0 x4 0.1 x5 0.2 x6 0.3 x7 ...

数据由多个数据集组成,每个数据集的第一列均以0开头(因此x1,x2,x3是一个集合,x4,x5,x6,x7是另一个集合).我需要分别绘制每个数据集,所以我需要以某种方式拆分数据.最简单的方法是什么?

The data consists of multiple datasets, each starting with 0 in the first column (so x1,x2,x3 would be one set and x4,x5,x6,x7 another one). I need to plot each dataset separately so I need to somehow split the data. What would be the easiest way to accomplish this?

我意识到我可以逐行浏览数据并在第一列中遇到0时拆分数据,但这似乎效率很低.

I realize I could go through the data line-by-line and split the data every time I encounter a 0 in the first column but this seems very inefficient.

推荐答案

我实际上很喜欢本杰明的答案,一个简短的解决方案是:

I actually liked Benjamin's answer, a slightly shorter solution would be:

B= np.split(A, np.where(A[:, 0]== 0.)[0][1:])

更多推荐

以numpy中的值拆分数组

本文发布于:2023-11-30 15:25:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!