# numpy 找到连续的序列的标号

import numpy as np

def zero_runs(a):
# Create an array that is 1 where a is 0, and pad each end with an extra 0.
iszero = np.concatenate((, np.equal(a, 0).view(np.int8), ))
absdiff = np.abs(np.diff(iszero))
# Runs start and end where absdiff is 1.
ranges = np.where(absdiff == 1).reshape(-1, 2)
return ranges

def one_runs(a):
# Create an array that is 1 where a is 0, and pad each end with an extra 0.
iszero = np.concatenate((, np.equal(a, 1).view(np.int8), ))
absdiff = np.abs(np.diff(iszero))
# Runs start and end where absdiff is 1.
ranges = np.where(absdiff == 1).reshape(-1, 2)
return ranges

def two_runs(a):
# Create an array that is 1 where a is 0, and pad each end with an extra 0.
iszero = np.concatenate((, np.equal(a, 2).view(np.int8), ))
absdiff = np.abs(np.diff(iszero))
# Runs start and end where absdiff is 1.
ranges = np.where(absdiff == 1).reshape(-1, 2)
return ranges

def three_runs(a):
# Create an array that is 1 where a is 0, and pad each end with an extra 0.
iszero = np.concatenate((, np.equal(a, 3).view(np.int8), ))
absdiff = np.abs(np.diff(iszero))
# Runs start and end where absdiff is 1.
ranges = np.where(absdiff == 1).reshape(-1, 2)
return ranges

def four_runs(a):
# Create an array that is 1 where a is 0, and pad each end with an extra 0.
iszero = np.concatenate((, np.equal(a, 4).view(np.int8), ))
absdiff = np.abs(np.diff(iszero))
# Runs start and end where absdiff is 1.
ranges = np.where(absdiff == 1).reshape(-1, 2)
return ranges

if __name__=='__main__':
a = [1, 1, 0, 0, 1, 2, 2, 3, 3, 3, 0, 0, 4, 4, 0, 4, 4, 3, 3, 2, 2, 1, 1, 2, 2, 0, 1, 1]
runs = zero_runs(a)
print(runs)

runs = one_runs(a)
print(runs)

runs = two_runs(a)
print(runs)

runs = three_runs(a)
print(runs)

runs = four_runs(a)
print(runs)


09-09 1万+

#### 用python编写一个时时彩的开奖号码采集器

10-18 121

#### 128. 最长连续序列 07-14 1万+

#### Python读取WAV文件的几种方式整理

01-09 9767

#### python排序、得出序号各类方法大全 numpy pandas

05-22 1024

#### 数据分析笔记--numpy--ndarray的序列创建

02-16 4346

#### 查找连续相同值的算法，并给出连续相同值的个数以及位置

09-12 632

#### 数据分析汇总(NumPy篇)

06-10 195

#### leetcod：最长连续序列（python）

05-29 222

#### numpy库：常用基本

08-07 1338

#### 在数组中需找连续“1”的起始位置和结束位置 ©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 点击重新获取   扫码支付 1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。 余额充值