Python numpy.defchararray.center()

Python numpy.defchararray.center()

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

参数:
arr :类似数组或字符串。
width :结果字符串的长度。
fillchar :填充字符。默认为空格。
返回值 :带中心的字符串的数组的副本。

代码 #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'

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程