在Python中的原地操作符
Python提供了一组原地操作符。原地操作符允许您在变量上执行算术、逻辑和位操作,以原地更新其值。这些操作符通过将赋值操作符(=)与另一个操作符结合来表示。它可以提高程序的性能并节省内存。
在本文中,我们将探讨Python中提供的各种原地操作符,并附带示例。
1. iadd() – 原地加法
iadd(x, y)操作符执行一次原地加法操作。它使用结果更新变量的值。它由+=符号表示。
import operator
# Variables
a = 5
b = 4
print("Original, a =", a)
# In-place Addition
a += b
# The above operation is equivalent to
# a = a + b
# a = operator.iadd(a, b)
# a = operator.__iadd__(a, b)
print("Updated, a =", a)
输出:
Original, a = 5
Updated, a = 9
上述代码演示了如何进行原地加法。
2. isub() – 原地减法
isub(x, y)运算符执行原地减法运算。它将变量的值更新为结果。它由-=符号表示。
import operator
# Variables
a = 5
b = 4
print("Original a =", a)
# In-place Subtraction
a -= b
# The above operation is equivalent to
# a = a - b
# a = operator.isub(a, b)
# a = operator.__isub__(a, b)
print("Updated a =", a)
输出:
Original, a = 5
Updated, a = 1
上面的代码示例演示了如何进行原位减法。
3. imul() – 原位乘法
imul(x, y) 运算符执行一个原位乘法操作。它更新变量的值并将结果存储在变量中。它用 *= 符号表示。
import operator
# Variables
a = 5
b = 4
print("Original, a =", a)
# In-place Multiplication
a *= b
# The above operation is equivalent to
# a = a * b
# a = operator.imul(a, b)
# a = operator.__imul__(a, b)
print("Updated, a =", a)
输出:
Original, a = 5
Updated, a = 20
上述代码演示了如何执行原地乘法。
4. itruediv() – 原地真除法
itruediv(x, y) 运算符执行一个原地真除法操作。它用结果更新变量的值。它用 /= 符号表示。
import operator
# Variables
a = 5
b = 4
print("Original, a =", a)
# In-place true division
a /= b
# The above operation is equivalent to
# a = a / b
# a = operator.itruediv(a, b)
# a = operator.__itruediv__(a, b)
print("Updated, a =", a)
输出:
Original, a = 5
Updated, a = 1.25
以上代码演示了如何执行原地真除法。
5. iand(),ior(),ixor() – 原位按位与,按位或,按位异或
iand(x,y)运算符执行原地按位与操作。它由&=符号表示。
ior(x,y)运算符执行原位按位或操作。它由|=符号表示。
ixor(x,y)运算符执行原位按位异或操作。它由^=符号表示。
它们都通过更新变量的值来得到结果。
import operator
# Variables
a = 5
b = 4
c = 3
d = 2
print("Original, a =", a)
print("Original, c =", c)
print("Original, d =", d)
# In-place Bitwise AND
a &= b
# The above operation is equivalent to
# a = a & b
# a = operator.iand(a, b)
# a = operator.__iand__(a, b)
# In-place Bitwise OR
c |= b
# The above operation is equivalent to
# c = c | b
# c = operator.iand(c, b)
# c = operator.__iand__(c, b)
# In-place Bitwise XOR
d ^= b
# The above operation is equivalent to
# d = d ^ b
# d = operator.iand(d, b)
# d = operator.__iand__(d, b)
print("Updated, a =", a)
print("Updated, c =", c)
print("Updated, d =", d)
输出:
Original, a = 5
Original, c = 3
Original, d = 2
Updated, a = 4
Updated, c = 7
Updated, d = 6
以上代码演示了如何执行原地AND、OR和XOR操作。
6. imod()和ifloordiv() – 原地取模和向下取整除法
imod(x, y)运算符执行原地取模操作。它由%=符号表示。
ifloordiv(x, y)运算符执行原地向下取整除法操作。它由//=符号表示。
它们通过结果来更新变量的值。
import operator
# Variables
a = 5
b = 4
c = 15
print("Original, a =", a)
print("Original, c =", c)
# In-place modulo
a %= b
# The above operation is equivalent to
# a = a % b
# a = operator.imod(a, b)
# a = operator.__imod__(a, b)
# In-place floor division
c //= b
# The above operation is equivalent to
# c = c // b
# c = operator.ifloordiv(c, b)
# c = operator.__ifloordiv__(c, b)
print("Updated, a =", a)
print("Updated, c =", c)
输出:
Original, a = 5
Original, c = 15
Updated, a = 1 # Remainder when 5 is divided by 4
Updated, c = 3 # Floor value of the result
上面的代码演示了如何执行原地取模和向下取整除法操作。
7. ilshift() 和 irshift() – 原地左移和右移
ilshift(x, y) 运算符执行原地左移操作。它用 << = 符号表示。
irshift(x, y) 运算符执行原地右移操作。它用 >> = 符号表示。
它们更新变量的值为结果。
import operator
# Variables
a = 20
b = 25
print("Original, a =", a)
print("Original, b =", b)
# In-place left shift
a <<= 2 # Shifts two bits to the left
# The above operation is equivalent to
# a = a << 2
# a = operator.ilshift(a, 2)
# a = operator.__ilshift__(a, 2)
# In-place right shift
b >>= 2 # Shifts two bits to the right
# The above operation is equivalent to
# b = b >> 2
# b = operator.irshift(b, 2)
# b = operator.__irshift__(b, 2)
print("Updated, a =", a)
print("Updated, b =", b)
输出:
Original, a = 20
Original, b = 25
Updated, a = 80
Updated, b = 6
上述代码演示了如何进行原地取模和地板除法操作。
8. ipow() – 原地指数运算
ipow(x, y) 操作符执行一个原地指数运算操作。它由 **= 符号表示。它通过结果更新变量的值。
import operator
# Variables
a = 10
b = 2
print("Original, a =", a)
# In-place exponentiation
a **= b
# The above operation is equivalent to
# a = a ** b
# a = operator.ipow(a, b)
# a = operator.__ipow__(a, b)
print("Updated, a =", a)
输出:
Original, a = 10
Updated, a = 100
上面的代码演示了如何进行原地幂运算。
9. imatmul() – 原地矩阵乘法
imatmul()运算符执行了原地矩阵乘法操作。它更新变量的值为结果。它用@=符号表示。
import operator
import numpy as np
# Numpy Arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print("Original, a =", a)
# In-place exponentiation
a @= b
# The above operation is equivalent to
# a = a @ b
# a = operator.imatmul(a, b)
# a = operator.__imatmul__(a, b)
print("Updated, a =", a)
输出:
Original, a = [[1 2] [3 4]]
Updated, a = [[19 22] [43 50]]
上面的代码演示了如何执行就地矩阵乘法操作。
10. iconcat() – 就地连接
iconcat(x, y)执行就地连接操作。它用 += 符号表示,并用于连接字符串、列表和其他序列。它通过将变量的值更新为连接的结果来完成操作。
import operator
# Example with strings
s = "Hello"
print("Original, s =", s)
s += " World"
# Equivalent to s = s + " World"
# Equivalent to s = operator.iconcat(s, " World")
# Equivalent to s = operator.__iconcat__(s, " World")
print("Updated, s =", s)
# Example with lists
numbers = [1, 2, 3]
print("Original, numbers =", numbers)
numbers += [4, 5]
# Equivalent to numbers = numbers + [4, 5]
# Equivalent to numbers = operator.iconcat(numbers, [4, 5])
# Equivalent to numbers = operator.__iconcat__(numbers, [4, 5])
print("Updated, numbers =", numbers)
输出:
Original, s = Hello
Updated, s = Hello World
Original, numbers = [1, 2, 3]
Updated, numbers = [1, 2, 3, 4, 5]
上面的代码演示了如何执行原地连接操作。
结论
总之,原地操作符允许您在原地执行不同的操作,即更新变量的值而不创建新的变量或对象。它们提供了一种高效的执行操作、节省内存并使您的代码更易读的方式。通过使用这些原地操作符,您可以简化代码并提高其性能。