當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。