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


Python numpy.defchararray.center()用法及代码示例


numpy.core.defchararray.center(arr, width, fillchar)将字符串的元素按元素居中。

参数:
arr : array-like or string.
width : Length og resulting string.
fillchar : Padding character. Default is space.

返回: Copy of array with centerd string.



代码1:

# Python Program illustrating  
# numpy.char.center() method  
import numpy as np  
  
arr1 = ['eAAAa', 'ttttds', 'AAtAAt'] 
print ("arr1:", arr1) 
  
print ("\narr1:", np.char.center(arr1, 7, fillchar ='z')) 
print ("\narr1:", np.char.center(arr1, [9, 9, 11], 
                         fillchar =['z', '1', '3'])) 
  
print ("\narr1:", np.char.center(arr1, 11, fillchar ='z'))

输出:

arr1: ['eAAAa', 'ttttds', 'AAtAAt']

 centered arr1: ['zeAAAaz' 'zttttds' 'zAAtAAt']

 centered arr1: ['zzeAAAazz' '11ttttds1' '333AAtAAt33']

 centered arr1: ['zzzeAAAazzz' 'zzzttttdszz' 'zzzAAtAAtzz']


代码2:

# Python Program illustrating  
# numpy.char.center() method  
import numpy as np  
  
arr2 = ['11sf', 'sdsf2', '1111f2'] 
print ("arr2:", arr2) 
  
# Will throw Error 
print ("\narr2:", np.char.center(arr2))

输出:

arr2: ['11sf', 'sdsf2', '1111f2']

TypeError: center() missing 1 required positional argument:'width'


相关用法


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