例子
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print 'First array:'
print a
print '\n'
print 'Axis parameter not passed. The input array is flattened before insertion.'
print np.insert(a,3,[11,12])
print '\n'
print 'Axis parameter passed. The values array is broadcast to match input array.'
print 'Broadcast along axis 0:'
print np.insert(a,1,[11],axis = 0)
print '\n'
print 'Broadcast along axis 1:'
print np.insert(a,1,11,axis = 1)
它的输出如下 -
First array:
[[1 2]
[3 4]
[5 6]]
Axis parameter not passed. The input array is flattened before insertion.
[ 1 2 3 11 12 4 5 6]
Axis parameter passed. The values array is broadcast to match input array.
Broadcast along axis 0:
[[ 1 2]
[11 11]numpy.insert
[ 3 4]
[ 5 6]]
Broadcast along axis 1:
[[ 1 11 2]
[ 3 11 4]
[ 5 11 6]]