栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

算法Python numpy作用(numpy.diff、numpy.argwhere)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

算法Python numpy作用(numpy.diff、numpy.argwhere)

调用
import numpy as np
函数介绍(numpy.diff)

函数:numpy.diff

参数:
arr : [array_like] Input array.
n : [int, optional] The number of times values are differenced.
axis : [int, optional] The axis along which the difference is taken, default is the last axis.

返回: [ndarray]The n-th discrete difference. The output is the same as a except along axis where the dimension is smaller by n.

作用

判断一串数据是否有连续性,例如1,2,3,4,5,6,7,8或者a,b,c,d,e,f,g,在或者

0x01,0x02,0x03,0x04,0x05,0x06,再或者A,B,C,D,E,F,G等等

实例

字符串

4001676200000000c40a060000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f

转换ASCII

'@x01gbx00x00x00x00xc4nx06x00x00x00x00x00x10x11x12x13x14x15x16x17x18x19x1ax1bx1cx1dx1ex1f !"#$%&'()*+,-./0123456789:;<=>?'
diff_2 = np.diff(list(payload), n=2)
print(diff_2)


结果:
[ 165 -107  -93   98    0    0  196 -382  182   -2    6    0    0    0
   16  -15    0    0    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0]

总结:那些0既表示此数据有很多连续自增数

函数介绍(numpy.argwhere)

函数名:numpy.argwhere

参数:
arr :[数组]输入数组。

返回:[ndarray]非零元素的索引。索引按元素分组。

作用

返回数组中,非0的坐标,常用于获取数组中非0的下表,或者非0个数

实例

数组

[ 165 -107  -93   98    0    0  196 -382  182   -2    6    0    0    0
   16  -15    0    0    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0]
print(np.argwhere(diff_2 != 0))


结果:
[[ 0]
 [ 1]
 [ 2]
 [ 3]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]
 [14]
 [15]]


print(len(np.argwhere(diff_2 != 0)))


结果:
11
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号