位运算(了解即可)

序号 函数 位运算符 描述说明
1 bitwise_and & 计算数组元素之间的按位与运算。
2 bitwise_or | 计算数组元素之间的按位或运算。
3 invert ~ 计算数组元素之间的按位取反运算。
4 left_shift << 将二进制数的位数向左移。
5 right_shift >> 将二进制数的位数向右移。

bitwise_and(按位与)

该函数对数组中整数的二进制数进行“按位与”运算

  1. import numpy as np
  2. a = 10
  3. b = 12
  4. print("a的二进制数:",bin(a))
  5. print("b的二进制数:",bin(b))
  6. print("将a与b执行按位与操作:",np.bitwise_and(a,b))
  7. # 输出结果如下:
  8. """
  9. a的二进制: 0b1010
  10. b的二进制: 0b1100
  11. a与b执行按位与操作: 8
  12. """

bitwise_or(按位或)

该函数对数组中整数的二进制数进行“按位或”运算

  1. import numpy as np
  2. a,b = 13,17
  3. print ('13 和 17 的二进制数:')
  4. print (bin(a), bin(b))
  5. print ('13 和 17 的位或:')
  6. print (np.bitwise_or(13, 17))

invert(按位取反)

该函数对数组中整数的二进制数进行“按位取反”运算

  • 若是有符号的负整数,取其二进制数的补码,并执行 +1 操作
    1. 对于有符号二进制数,其最高位为 0 表示正数;最高位为 1 表示负数。
    ```python import numpy as np

数据类型为无符号整型uint8

arr = np.array([20],dtype = np.uint8) print(“二进制表示:”,np.binary_repr(20,8)) print(np.invert(arr))

进行取反操作

print(“二进制表示: “, np.binary_repr(235,8))

输出结果如下:

“”” 二进制表示:00010100 [235] 二进制表示:11101011 “””

  1. <a name="qZQqx"></a>
  2. ## `left_shift`(左移)
  3. 该方法把数组元素的二进制数向左移动到指定位置,而其返回值所对应的二进制数,则会从右侧追加相等数量的 `0`(移动了多少位便追加多少个`0`)。
  4. ```python
  5. import numpy as np
  6. #移动三位后的输出值
  7. print (np.left_shift(20,3)
  8. #打印移动后20的二进制数
  9. print (np.binary_repr(20, width = 8))
  10. #函数返回值的二进制数
  11. print (np.binary_repr(160, width = 8))

right_shift(右移)

right_shift()将数组中元素的二进制数向右移动到指定位置,其返回值对应的二进制数会从左侧追加相等数量的 0。该函数使用与left_shift() 恰好相反。

  1. import numpy as np
  2. #将40右移两位后返回值:
  3. print (np.right_shift(40,2))
  4. #移动后40的二进制数:
  5. print (np.binary_repr(40, width = 8))
  6. #移动后返回值的二进制数:
  7. print (np.binary_repr(10, width = 8))

字符串处理函数

函数名称 描述
add() 对两个数组相应位置的字符串做连接操作。
multiply() 返回多个字符串副本,比如将字符串“ hello”乘以3,则返回字符串“ hello hello hello”。
center() 用于居中字符串,并将指定的字符,填充在原字符串的左右两侧。
capitalize() 将字符串第一个字母转换为大写。
title() 标题样式,将每个字符串的第一个字母转换为大写形式。
lower() 将数组中所有的字符串的大写转换为小写。
upper() 将数组中所有的字符串的小写转换为大写。
split() 通过指定分隔符对字符串进行分割,并返回一个数组序列,默认分隔符为空格。
splitlines() 以换行符作为分隔符来分割字符串,并返回数组序列。
strip() 删除字符串开头和结尾处的空字符。
join() 返回一个新的字符串,该字符串是以指定分隔符来连接数组中的所有元素。
replace() 用新的字符串替换原数组中指定的字符串。
decode() 用指定的编码格式对数组中元素依次执行解码操作。
encode() 用指定的编码格式对数组中元素依次执行编码操作。

add()

  1. import numpy as np
  2. print(np.char.add(['welcome','url'], [' to C net','is c.biancheng.net'] ))
  3. # 输出结果如下:
  4. """
  5. ['welcome to C net' 'url is c.biancheng.net']
  6. """

multiply()

  1. import numpy as np
  2. print (np.char.multiply('c.biancheng.net',3))
  3. # 输出结果如下:
  4. """
  5. c.biancheng.net c.biancheng.net c.biancheng.net
  6. """

center()

  1. np.char.center(string, width, fillchar)
  2. 其中:
  3. * string:表示字符串
  4. * width:表示长度
  5. * fillchar:表示要填充的字符
  1. import numpy as np
  2. print(np.char.center("c.bianchneg.net", 20, '*'))
  3. # 输出结果如下:
  4. """
  5. **c.bianchneg.net***
  6. """

capitalize()

  1. import numpy as np
  2. print (np.char.capitalize('python'))
  3. # 输出结果如下:
  4. """
  5. Python
  6. """

title()

  1. import numpy as np
  2. print(np.char.title("welcome to china"))
  3. # 输出结果如下:
  4. """
  5. Welcome To China
  6. """

lower()

  1. import numpy as np
  2. print(np.char.lower("WELCOME TO MYHOME"))
  3. # 输出结果如下:
  4. """
  5. welcome to myhome
  6. """

upper()

  1. import numpy as np
  2. print(np.char.upper("Welcome To Python"))
  3. # 输出结果如下:
  4. """
  5. WELCOME TO JAVATPOINT
  6. """

split()

  1. import numpy as np
  2. print(np.char.split("Welcome To Python"),sep = " ")
  3. # 输出结果如下:
  4. """
  5. ['Welcome', 'To', 'Python']
  6. """

splitlines()

  1. import numpy as np
  2. print("Splitting the String line by line..")
  3. print(np.char.splitlines("Welcome\nTo\nPython"))
  4. # 输出结果如下:
  5. """
  6. Splitting the String line by line..
  7. ['Welcome', 'To', 'Python']
  8. """

strip()

  1. import numpy as np
  2. print("原字符串:",str)
  3. str = " welcome to Python "
  4. print(np.char.strip(str))
  5. # 输出结果如下:
  6. """
  7. 原字符串: <class 'str'>
  8. welcome to Python
  9. """

join()

  1. import numpy as np
  2. print (np.char.join(':','Love'))
  3. #也可指定多个分隔符
  4. print (np.char.join([':','-'],['Love','Python']))
  5. # 输出结果如下:
  6. """
  7. L:o:v:e
  8. ['L:o:v:e' 'P-y-t-h-o-n']
  9. """

replace()

  1. import numpy as np
  2. str = "Welcome to China"
  3. print("原字符串:",str)
  4. #更改后字符串
  5. print(np.char.replace(str, "Welcome to","Hello"))
  6. # 输出结果如下:
  7. """
  8. 原字符串: Welcome to China
  9. Hello China
  10. """

encode()/decode()

默认以**utf-8**的形式进行编解码

  1. import numpy as np
  2. #cp500国际编码
  3. encode_str = np.char.encode("Welcome to China", 'cp500')
  4. decode_str =np.char.decode(encode_str, 'cp500')
  5. print(encode_str)
  6. print(decode_str)
  7. # 输出结果如下:
  8. """
  9. b'\xa6\x85\x93\x83\x96\x94\x85@\xa3\x96@\xc3\x88\x89\x95\x81'
  10. Welcome to China
  11. """