bitwise_and()
函数对两个数组中整数的二进制形式执行位与运算。import numpy as np
a = np.random.randint(1,11,(3,4))
print(a)
for i in a:
for j in i:
print(bin(j),end=",") # python 的 bin 函数可以将整数转为二进制数
print()
b = np.random.randint(1,11,(3,4))
print(b)
for i in b:
for j in i:
print(np.binary_repr(j),end=",") # np.binary_repr 函数可以将整数转为二进制数
print()
print(np.bitwise_and(a,b))
bitwise_or()
函数对两个数组中整数的二进制形式执行位或运算。用法同上。invert()
方法对数组中整数做按位取反运算,也就是 0 变成 1,1 变为 0。注:计算机中都是使用补码存储数字,所以按位取反都是取补码的相反值。
import numpy as np
a = np.random.randint(-5,6,(3,4))
print(a)
for i in a:
for j in i:
print(bin(j),end=",")
print()
b = np.invert(a)
print(b)
for i in b:
for j in i:
print(np.binary_repr(j),end=",")
print()
-3
的补码11111101
按位取反00000010
就是2
;5
的补码即原码00000101
按位取反11111010
,转十进制即为-6
。left_shift(arr, step)
函数将数组 arr 中的每个元素的二进制形式向左移动 step 位,右侧以 0 填充。(左移二进制数 n n n 位相当于原数字乘以 2 n 2^n 2n)import numpy as np
a = np.random.randint(-5,6,(3,4))
print(a)
for i in a:
for j in i:
print(np.binary_repr(j),end=",")
print()
b = np.left_shift(a,2)
print(b)
for i in b:
for j in i:
print(np.binary_repr(j),end=",")
print()
right_shift(arr, step)
函数将数组 arr 中的每个元素的二进制形式向右移动 step 位,左侧补 0。(右移二进制数 n n n 位相当于原数字除以 2 n 2^n 2n 向下取整)。注:负数位数不够会变成-1
,正数位数不够会变成0
。import numpy as np
a = np.random.randint(-5,16,(3,4))
print(a)
for i in a:
for j in i:
print(np.binary_repr(j),end=",")
print()
b = np.right_shift(a,2)
print(b)
for i in b:
for j in i:
print(np.binary_repr(j),end=",")
print()
\qquad NumPy
提供了许多字符串处理函数,它们被定义在用于处理字符串数组的 numpy.char
类中,这些函数的操作对象是 string_
或者 unicode_
类型的字符串数组。
numpy.char.add(x1, x2)
函数依次对 x 1 x_1 x1 和 x 2 x_2 x2 两个数组的元素进行字符串连接,数组 x 1 x_1 x1 和 x 2 x_2 x2 必须具有相同的形状。示例如下:import numpy as np
a = np.random.randint(1,11,(3,8)).astype(np.str_)
b = np.random.randint(11,21,(3,8)).astype(np.str_)
print(a)
print(b)
print(np.char.add(a,b))
numpy.char.multiply(arr, i)
该函数将 a r r arr arr 数组进行 i i i 次连接。示例如下:import numpy as np
a = np.random.randint(1,11,(3,8)).astype(np.str_)
print(a)
print(np.char.multiply(a,3))
np.char.center(string, width, fillchar)
函数用于将 string
字符串在 width
宽度居中,并使用指定字符 fillchar
在左侧和右侧进行填充。示例如下:import numpy as np
a = np.random.randint(1,11,(3,8)).astype(np.str_)
print(a)
print(np.char.center(a,5,"!"))
numpy.char.ljust(a, width[, fillchar=' '])
函数用于将 string
字符串在 width
宽度左对齐,并使用指定字符 fillchar
在右侧进行填充。示例如下:import numpy as np
a = np.random.randint(1,11,(3,8)).astype(np.str_)
print(a,"\n")
print(np.char.ljust(a,5,"!"))
numpy.char.rjust(a, width[, fillchar=' '])
函数用于将 string
字符串在 width
宽度右对齐,并使用指定字符 fillchar
在左侧进行填充。示例如下:import numpy as np
a = np.random.randint(1,11,(3,8)).astype(np.str_)
print(a,"\n")
print(np.char.rjust(a,5,"!"))
numpy.char.capitalize(a)
函数将字符串 a 的第一个字母转换为大写。示例如下:import numpy as np
import random
li = ['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,11,(3,8)).astype(np.str_) # 生成 3 行 8 列的 str_ 类型数组
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 5)) # 将数组中的每个元素变成 5 个字母的随机字符串
print(unicode_str,"\n")
print(np.char.capitalize(unicode_str)) # 将字符串的第一个字母转换为大写
numpy.char.title()
将字符串数组中每个元素的第一个字母转换为大写。注意:如果一个字符串中间有非字母,则非字母之后的字符串会被视为另一个单词。示例如下:import numpy as np
import random
li = [' ','9','8','7','6','5','4','3','2','1','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.title(unicode_str))
numpy.char.lower()
函数将字符串数组中每个元素转换为小写。示例如下:import numpy as np
import random
li = ['Z','Y','X','W','V','U','T','S','R','Q','P','O','N','m','l','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.lower(unicode_str))
numpy.char.upper()
函数将字符串数组中每个元素转换为大写。示例如下:import numpy as np
import random
li = ['Z','Y','X','W','V','U','T','S','R','Q','P','O','N','m','l','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.upper(unicode_str))
numpy.char.swapcase()
函数将数组中每个元素字母大写转为小写,小写转为大写。示例如下:import numpy as np
import random
li = ['Z','Y','X','W','V','U','T','S','R','Q','P','O','N','m','l','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.swapcase(unicode_str))
numpy.char.zfill(a, width)
函数表示如果宽度 width
大于原字符串 a
的宽度,则在数组元素的左边填充 0 以达到 width
;如果宽度 width
小于原字符串 a
的宽度,则结果会去掉原字符串中多余的元素。示例如下:import numpy as np
import random
li = ['Z','Y','X','W','V','U','T','S','R','Q','P','O','N','m','l','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 10))
print(unicode_str,"\n")
print(np.char.zfill(unicode_str,6),"\n") # 宽度 6 小于原字符串 unicode_str 的宽度,则结果会去掉原字符串中多余的元素
print(np.char.zfill(unicode_str,10)) # 宽度 10 大于原字符串 unicode_str 的宽度,则在数组元素的左边填充 0 以达到 10
numpy.char.split(a[, sep=None, maxsplit=None])
函数通过指定分隔符对字符串进行分割,从最左边的分割符开始分割,可指定分割次数 maxsplit
,返回 list
列表。默认情况下,分隔符为空格。示例如下:import numpy as np
import random
li = [' ','4','3','2','1','4','3','2','1','d','c','b','a','d','c','b','a','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.split(unicode_str,"b",1))
numpy.char.split(a[, sep=None, maxsplit=None])
函数通过指定分隔符对字符串进行分割,从最右边的分割符开始分割,可指定分割次数 maxsplit
,返回 list
列表。默认情况下,分隔符为空格。示例如下:import numpy as np
import random
li = [' ','4','3','2','1','4','3','2','1','d','c','b','a','d','c','b','a','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.rsplit(unicode_str,"b",1))
numpy.char.splitlines(a[, keepends=None])
以换行符(\n,\r,\r\n
)作为分隔符来分割字符串,并返回一个数组序列。如果指定 keepends
为 True
,则换行符会包含在结果列表中,否则不包含。示例如下:import numpy as np
import random
li = ['\n','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.splitlines(unicode_str),"\n")
print(np.char.splitlines(unicode_str,keepends=True))
numpy.char.partition(a, sep)
函数通过指定分割符 sep
对字符串 a
进行分割。如果元素包含多个分割符,则从最左边第一次出现的分割符开始分割,仅分割一次,返回三个元素:sep
前的字符串、分割字符sep
和 sep
后的字符;如果找不到分隔符,则返回三个元素: 字符串本身以及两个空字符串。示例如下:import numpy as np
import random
li = [' ','4','3','2','1','4','3','2','1','d','c','b','a','d','c','b','a','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.partition(unicode_str,"b"))
numpy.char.rpartition(a, sep)
函数通过指定分割符 sep
对字符串 a
进行分割。如果元素包含多个分割符,则从最右边第一次出现的分割符开始分割,仅分割一次,返回三个元素:sep
前的字符串、分割字符sep
和 sep
后的字符;如果找不到分隔符,则返回三个元素: 两个空字符串以及字符串本身。示例如下:import numpy as np
import random
li = [' ','4','3','2','1','4','3','2','1','d','c','b','a','d','c','b','a','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.rpartition(unicode_str,"b"))
numpy.char.strip(a[, chars=None])
函数用于移除数组每个元素开头和结尾处的空格。如果参数 chars 省略或者为 None,则默认为删除空白。示例如下:import numpy as np
import random
li = [' ','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 10))
print(unicode_str,"\n")
print(np.char.strip(unicode_str))
numpy.char.lstrip(a[, chars=None])
函数用于移除数组每个元素最左边的特定字符。如果参数 chars 省略或者为 None,则默认为删除空白。示例如下:import numpy as np
import random
li = [' ','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 10))
print(unicode_str,"\n")
print(np.char.lstrip(unicode_str))
numpy.char.rstrip(a[, chars=None])
函数用于移除数组每个元素最右边的特定字符。如果参数 chars 省略或者为 None,则默认为删除空白。示例如下:import numpy as np
import random
li = [' ','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 10))
print(unicode_str,"\n")
print(np.char.rstrip(unicode_str))
numpy.char.join(sep,arr)
函数通过指定分隔符 sep 来连接 arr 数组中的元素或字符串,其中 sep 是 string_
或者 unicode_
类型的字符串数组。示例如下:import numpy as np
import random
li = ['k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 10))
print(unicode_str,"\n")
print(np.char.join([":","-",",","、"],unicode_str))
numpy.char.replace(a, old, new[, count=None])
函数可以使用新字符串 new 来替换原字符串 a 中的子字符串 old。
count
:int 类型,如果指定该值 N,则会替换 old 中出现的前 N 个字符串。import numpy as np
import random
li = ['d','c','b','a','d','c','b','a','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 10))
print(unicode_str,"\n")
print(np.char.replace(unicode_str,'d','D',2))
numpy.char.translate(a, table[, deletechars=None])
函数将数组元素字符串 a
按照给定的字符映射转换表 table
进行转换。如果指定了 deletechars
列表,则先移除 deletechars
包含的字符然后进行转换。
str.maketrans()
方法转换而来,该方法返回一个由转换前后字符的 ASCII
值组成的键值对所构造的字典,如果设置了 delchars
参数,则 delchars
中每个转换后的值为 None
。maketrans()
方法语法格式如下:str.maketrans(intab,outtab[,delchars])
intab
:需要转换的字符组成的字符串。outtab
:转换的目标字符组成的字符串。delchars
:可选参数,表示要删除的字符组成的字符串。a
和 b
转换为 A
和 B
,删除字符 c
和 d
。import numpy as np
import random
li = [' ','4','3','2','1','4','3','2','1','d','c','b','a','d','c','b','a','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
intab = "ab"
outtab = "AB"
table = str.maketrans(intab,outtab,"cd")
print(table,"\n")
print(np.char.translate(unicode_str,table))
numpy.char.encode(a[, encoding=None, errors=None])
函数用于编码操作,数组元素依次调用 str.encode
,可以使用 Python
标准库中的编解码器。
a
:要处理的 str
或 unicode
数组。encoding
:编码名称,可选项,str
类型,默认编码为 utf-8
。errors
:指定如何处理编码错误,可选项,str
类型。默认为 'strict'
,意为编码错误引起一个 UnicodeError
。 其他可能得值有 'ignore'
、'replace'
、'xmlcharrefreplace'
、'backslashreplace'
以及通过 codecs.register_error()
注册的任何值。import numpy as np
import random
li = ['p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
print(np.char.encode(unicode_str,'cp037'))
numpy.char.encode(a[, encoding=None, errors=None])
函数用于解码操作,数组元素依次调用 str.decode
,可以使用 Python
标准库中的编解码器。
a
:要处理的 str
或 unicode
数组。encoding
:编码名称,可选项,str
类型,默认编码为 utf-8
。errors
:指定如何处理编码错误,可选项,str
类型。默认为 'strict'
,意为编码错误引起一个 UnicodeError
。 其他可能得值有 'ignore'
、'replace'
、'xmlcharrefreplace'
、'backslashreplace'
以及通过 codecs.register_error()
注册的任何值。import numpy as np
import random
li = ['p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a']
unicode_str = np.random.randint(1,10,(3,4)).astype(np.str_)
for us in np.nditer(unicode_str,op_flags=['readwrite']):
us[...] = ''.join(random.sample(li, 15))
print(unicode_str,"\n")
en_str = np.char.encode(unicode_str,'cp037')
print(en_str,"\n")
print(np.char.decode(en_str,'cp037'))
numpy.char.mod(a, values)
函数用于格式化字符串。当 a
是格式化语句时,将 values
中的元素逐个插入到格式化语句 a
中;当 a
是格式化语句组成的数组时,需要 values
和 a
满足广播规则。注:values
的元素值要符合格式化语句要求。示例如下:import numpy as np
print(np.char.mod('value=%d', np.arange(6)),"\n")
print(np.char.mod('value=%.4f', [[1.1, 2, 3.021], [4.12, 5, 6.1]]),"\n")
arr = [['value=%d','value=%f','value=%s'],['value=%d','value=%.2f','value=%.5f']]
print(np.char.mod(arr,3.1415926),"\n")
print(np.char.mod(arr,[[3.1415926],[6.57974576]]))