📜  Python中的 numpy.trim_zeros()

📅  最后修改于: 2022-05-13 01:54:53.541000             🧑  作者: Mango

Python中的 numpy.trim_zeros()

numpy.trim_zeros函数用于从一维数组或序列中修剪前导和/或尾随零。

代码 1:

import numpy as geek 
  
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
  
# without trim parameter
# returns an array without leading and trailing zeros 
  
res = geek.trim_zeros(gfg)
print(res)
Output :array([1, 5, 7, 0, 6, 2, 9, 0, 10])

代码 2:

import numpy as geek 
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
  
# without trim parameter
# returns an array without any leading  zeros 
  
res = geek.trim_zeros(gfg, 'f')
print(res)
Output :array([1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0])

代码 3:

import numpy as geek 
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
  
# without trim parameter
# returns an array without any trailing  zeros 
  
res = geek.trim_zeros(gfg, 'b')
print(res)
Output :array([0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10])