numpy 学习汇总9-字符操作( 基础学习 tcy)

Numpy字符操作 2018/11/20
        
===================================================================================
1.函数
    # 以下函数用于对dtype为string_或unicode_的数组执行向量化字符串操作。
    # 基于 Python 内置库中的标准字符串函数。
        
add()         #返回两个str或Unicode数组的逐个字符串连接
multiply()   #返回按元素多重连接后的字符串
center()     #返回给定字符串的副本,其中元素位于特定字符串的中央
capitalize() #返回给定字符串的副本,其中只有第一个字符串大写
title()         #返回字符串或 Unicode 的按元素标题转换版本
lower()      #返回一个数组,其元素转换为小写
upper()     #返回一个数组,其元素转换为大写
split()        #返回字符串中的单词列表,并使用分隔符来分割
splitlines()#返回元素中的行列表,以换行符分割
strip()       #返回数组副本,其中元素移除了开头或者结尾处的特定字符
join()        #返回一个字符串,它是序列中字符串的连接
replace()  #返回字符串的副本,其中所有子字符串的出现位置都被新字符串取代
decode()  #按元素调用str.decode
encode()  #按元素调用str.encode
        
===================================================================================
2.实例
np.char.add(['hello'],[' word'])            #字符串连接array(['hello word'], dtype='<U9')
np.char.multiply('Hello ',3)                  #多重连接array('Hello Hello Hello ', dtype='<U18')
    
# 此函数返回所需宽度的数组,以便输入字符串位于中心,并使用fillchar在左侧和右侧进行填充。
np.char.center('hello', 20,fillchar = '*')#array('*******hello********', dtype='<U20')
    
np.char.capitalize('hello world')     # 首字母大写array('Hello world', dtype='<U11')
np.char.title('hello how are you?') # 标题  词首字母大写array('Hello How Are You?', dtype='<U18')
np.char.lower(['HELLO','WORLD'])  # 小写array(['hello', 'world'], dtype='<U5')
np.char.upper(['hello','world'])       # 大写array(['HELLO', 'WORLD'], dtype='<U5')
    
np.char.split ('Tom,Bob,John', sep = ',')
# 指定分隔符分割字符串 array(list(['Tom', 'Bob', 'John']), dtype=object)
    
np.char.splitlines('hello\nhow are you?')# '\n','\r','\r\n'都会用作换行符。
#以换行符分割字符串 array(list(['hello', 'how are you?']), dtype=object)
np.char.splitlines('hello\rhow are you?')
#array(list(['hello', 'how are you?']), dtype=object)
    
np.char.strip(['aTom','admin','java'],'a')    #移除开头或结尾特定字符array(['Tom', 'dmin', 'jav'], dtype='<U5')
    
np.char.join(':','dmy')                                 #单个字符由特定的分隔符连接 array('d:m:y', dtype='<U5')
np.char.join([':','-'],['dmy','ymd'])                #array(['d:m:y', 'y-m-d'], dtype='<U5')
'.'.join(['ab', 'pq', 'rs'])                                # 'ab.pq.rs'
''.join(['ab', 'pq', 'rs'])                                 # 'abpqrs'
    
np.char.replace ('He is a good boy', 'is', 'was') # is 被was替代 array('He was a good boy', dtype='<U17')
    
np.char.encode('hello', 'cp500') #字符串编码 array(b'\x88\x85\x93\x93\x96', dtype='|S5')
char.encode()
# 此函数对数组中的每个元素调用str.encode函数。 默认编码是utf_8,可以使用标准 Python 库中的编解码器。
np.char.encode('hello', 'cp500') #array(b'\x88\x85\x93\x93\x96', dtype='|S5')
    =====================================================================================    

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/84311548
今日推荐