当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Numpy string lstrip()用法及代码示例


numpy.core.defchararray.lstrip(arr, chars=None) 是另一个在numpy中执行字符串操作的函数。它返回一个副本,其中删除了arr中每个元素的前导字符。

参数:
arr :str或unicode的数组。
char :[str或unicode,可选]要删除的字符集。如果省略或“无”,它将删除空格。 chars参数不是前缀或后缀;我们要剥离的是其值的所有组合。

返回:[ndarray] str或unicode的输出数组,具体取决于输入类型。


代码1:

# Python program explaining 
# numpy.char.lstrip() method  
  
import numpy as geek 
  
# input arrays   
in_arr = geek.array(['Sun', '  Moon  ', 'Star']) 
print ("Input array:", in_arr)  
  
out_arr = geek.char.lstrip(in_arr) 
  
# whitespace removed from arr[1] 
#  as we have set chars = None 
print ("Output array:", out_arr) 
输出:
Input array: ['Sun' ' Moon ' 'Star']
Output array: ['Sun' 'Moon' 'Star']


代码2:

# Python program explaining 
# numpy.char.lstrip() method  
  
import numpy as geek 
  
# input arrays  
in_arr = geek.array([ 'Geeks', 'For', 'Geeks'] ) 
print ("Input array:", in_arr)  
  
out_arr = geek.char.lstrip(in_arr, chars ='G') 
  
# 'G' removed from arr[0] and arr[2] 
# as we have set chars ='G' 
print ("Output array:", out_arr) 
输出:
Input array: ['Geeks' 'For' 'Geeks']
Output array: ['eeks' 'For' 'eeks']


代码3:

# Python program explaining 
# numpy.char.lstrip() method  
  
import numpy as geek 
  
# input arrays  
in_arr = geek.array([ 'Geeks', 'For', 'Geeks'] ) 
print ("Input array:", in_arr)  
  
out_arr = geek.char.lstrip(in_arr, chars ='s') 
print ("Output array:", out_arr)  
# Arrays remain unchanged as there are 
# no strings which start with 's'
输出:
Input array: ['Geeks' 'For' 'Geeks']
Output array: ['Geeks' 'For' 'Geeks']


相关用法


注:本文由纯净天空筛选整理自jana_sayantan大神的英文原创作品 numpy string operations | lstrip() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。