位运算(了解即可)
序号 | 函数 | 位运算符 | 描述说明 |
---|---|---|---|
1 | bitwise_and | & | 计算数组元素之间的按位与运算。 |
2 | bitwise_or | | | 计算数组元素之间的按位或运算。 |
3 | invert | ~ | 计算数组元素之间的按位取反运算。 |
4 | left_shift | << | 将二进制数的位数向左移。 |
5 | right_shift | >> | 将二进制数的位数向右移。 |
bitwise_and
(按位与)
该函数对数组中整数的二进制数进行“按位与”运算。
import numpy as np
a = 10
b = 12
print("a的二进制数:",bin(a))
print("b的二进制数:",bin(b))
print("将a与b执行按位与操作:",np.bitwise_and(a,b))
# 输出结果如下:
"""
a的二进制: 0b1010
b的二进制: 0b1100
a与b执行按位与操作: 8
"""
bitwise_or
(按位或)
该函数对数组中整数的二进制数进行“按位或”运算。
import numpy as np
a,b = 13,17
print ('13 和 17 的二进制数:')
print (bin(a), bin(b))
print ('13 和 17 的位或:')
print (np.bitwise_or(13, 17))
invert
(按位取反)
该函数对数组中整数的二进制数进行“按位取反”运算。
- 若是有符号的负整数,取其二进制数的补码,并执行 +1 操作。
```python import numpy as np对于有符号二进制数,其最高位为 0, 表示正数;最高位为 1, 表示负数。
数据类型为无符号整型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 “””
<a name="qZQqx"></a>
## `left_shift`(左移)
该方法把数组元素的二进制数向左移动到指定位置,而其返回值所对应的二进制数,则会从右侧追加相等数量的 `0`(移动了多少位便追加多少个`0`)。
```python
import numpy as np
#移动三位后的输出值
print (np.left_shift(20,3)
#打印移动后20的二进制数
print (np.binary_repr(20, width = 8))
#函数返回值的二进制数
print (np.binary_repr(160, width = 8))
right_shift
(右移)
right_shift()
将数组中元素的二进制数向右移动到指定位置,其返回值对应的二进制数会从左侧追加相等数量的 0
。该函数使用与left_shift()
恰好相反。
import numpy as np
#将40右移两位后返回值:
print (np.right_shift(40,2))
#移动后40的二进制数:
print (np.binary_repr(40, width = 8))
#移动后返回值的二进制数:
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()
import numpy as np
print(np.char.add(['welcome','url'], [' to C net','is c.biancheng.net'] ))
# 输出结果如下:
"""
['welcome to C net' 'url is c.biancheng.net']
"""
multiply()
import numpy as np
print (np.char.multiply('c.biancheng.net',3))
# 输出结果如下:
"""
c.biancheng.net c.biancheng.net c.biancheng.net
"""
center()
np.char.center(string, width, fillchar)
其中:
* string:表示字符串
* width:表示长度
* fillchar:表示要填充的字符
import numpy as np
print(np.char.center("c.bianchneg.net", 20, '*'))
# 输出结果如下:
"""
**c.bianchneg.net***
"""
capitalize()
import numpy as np
print (np.char.capitalize('python'))
# 输出结果如下:
"""
Python
"""
title()
import numpy as np
print(np.char.title("welcome to china"))
# 输出结果如下:
"""
Welcome To China
"""
lower()
import numpy as np
print(np.char.lower("WELCOME TO MYHOME"))
# 输出结果如下:
"""
welcome to myhome
"""
upper()
import numpy as np
print(np.char.upper("Welcome To Python"))
# 输出结果如下:
"""
WELCOME TO JAVATPOINT
"""
split()
import numpy as np
print(np.char.split("Welcome To Python"),sep = " ")
# 输出结果如下:
"""
['Welcome', 'To', 'Python']
"""
splitlines()
import numpy as np
print("Splitting the String line by line..")
print(np.char.splitlines("Welcome\nTo\nPython"))
# 输出结果如下:
"""
Splitting the String line by line..
['Welcome', 'To', 'Python']
"""
strip()
import numpy as np
print("原字符串:",str)
str = " welcome to Python "
print(np.char.strip(str))
# 输出结果如下:
"""
原字符串: <class 'str'>
welcome to Python
"""
join()
import numpy as np
print (np.char.join(':','Love'))
#也可指定多个分隔符
print (np.char.join([':','-'],['Love','Python']))
# 输出结果如下:
"""
L:o:v:e
['L:o:v:e' 'P-y-t-h-o-n']
"""
replace()
import numpy as np
str = "Welcome to China"
print("原字符串:",str)
#更改后字符串
print(np.char.replace(str, "Welcome to","Hello"))
# 输出结果如下:
"""
原字符串: Welcome to China
Hello China
"""
encode()/decode()
默认以**utf-8**
的形式进行编解码。
import numpy as np
#cp500国际编码
encode_str = np.char.encode("Welcome to China", 'cp500')
decode_str =np.char.decode(encode_str, 'cp500')
print(encode_str)
print(decode_str)
# 输出结果如下:
"""
b'\xa6\x85\x93\x83\x96\x94\x85@\xa3\x96@\xc3\x88\x89\x95\x81'
Welcome to China
"""