以numpy中的值拆分数组 [英] Split array at value in numpy

查看:77
本文介绍了以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中的值拆分数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆