clf*_*ter 7 python arrays optimization performance numpy
Input Array: Output Array:
[[1,2,3], [[1,2,3,3,3],
[0,0,0], -> repeat(2-times) -> [0,0,0,0,0],
[0,2,1]] [0,2,1,1,1]]
Run Code Online (Sandbox Code Playgroud)
x = np.array([[1,2,3],[0,0,0],[0,2,1]])
# to repeat last row two times two times
new_size = x.shape[1] + 2
new_x = np.zeros((3,new_size))
new_x[:,:3] = x
for i in range(x.shape[1],new_size):
new_x[:,i] = x[:,-1]
Run Code Online (Sandbox Code Playgroud)
一种可能的解决方案:
a = np.hstack((arr, np.tile(arr[:, [-1]], 2)))
print (a)
[[1 2 3 3 3]
[0 0 0 0 0]
[0 2 1 1 1]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3221 次 |
| 最近记录: |