算法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

'@\x01gb\x00\x00\x00\x00\xc4\n\x06\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./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

猜你喜欢

转载自blog.csdn.net/u012206617/article/details/124927435