网站建设中 模版/什么是百度快照
1. bisect
1.1 查找 bisect.bisect(array,item)
bisect是Python内置模块,主要用于有序序列的插入与查找!
使用这个模块的函数前先确保操作的列表是已排序的
查找 bisect(array, item)
若列表array中无item
bisect.bisect 系列返回的是插入索引的位置!
import bisect
a = [1, 8, 9, 10, 11, 15]
pt = bisect.bisect(a, 7)
print(pt)
print(a)
#结果:1
#结果:[1, 8, 9, 10, 11, 15]
若列表array中有item
bisect.bisect 查找将插入的重复元素索引位置为原元素的右边!
import bisect
a = [1, 8, 9, 10, 11, 15]
pt = bisect.bisect(a, 8)
print(pt)
print(a)
#结果:2
1.2 插入bisect. insort(array,item)
bisect.insort 系列返回的是插入排序后返回新的有序列表!
import bisect
a = [1, 8, 9, 10, 11, 15]
bisect.insort(a,7)
print(a)
#结果:[1, 7, 8, 9, 10, 11, 15]
2. numpy.diff()
import numpy as np
a=np.array([1, 6, 7, 8, 12])
diff_x1 = np.diff(a)
print("diff_x1",diff_x1)
#结果: diff_x1 [5 1 1 4]
从结果看,就是后一个值减去前一个值
3.numpy.cumsum()
numpy.cumsum(a, axis=None, dtype=None, out=None)
axis=0,按照行累加。
axis=1,按照列累加。
axis不给定具体值,就把numpy数组当成一个一维数组。
举例:
np.cumsum(a)
import numpy as np
a = np.array([[1,2,3], [4,5,6]])
print(np.cumsum(a))
#结果:array([ 1, 3, 6, 10, 15, 21])
np.cumsum(a,axis=0) #按照行累加,行求和
#结果:
array([[1, 2, 3],
[5, 7, 9]])
np.cumsum(a,axis=1) #按照列累加,列求和
#结果:
array([[ 1, 3, 6],
[ 4, 9, 15]])
4. append和extend的区别
list.append(object) 向列表中添加一个对象object
list.extend(sequence) 把一个序列seq的内容添加到列表中
a = [1, 2, 3]
b = [4, 5]
a.append(b)
print(a)
#结果:[1, 2, 3, [4, 5]]
使用append的时候, 是将b看作一个对象,整体打包添加到a中。
a = [1, 2, 3]
b = [4, 5]
a.extend(b)
print(a)
#结果:[1, 2, 3, 4, 5]
使用extend的时候,是将b看作一个序列,添加到a的后面。