Python| numpy numpy.ndarray.__invert__()
在Numpy numpy.ndarray.__invert__()
的帮助下,可以反转数组的元素。我们不必提供任何类型的参数,但请记住,此方法仅适用于整数值。
Syntax: ndarray.__invert__($self, /)
Return: ~self
示例 #1:
在这个例子中,我们可以看到数组中的每个元素都在ndarray.__invert__()
方法的帮助下对一个一元运算运算符进行运算,即~ 。
# import the important module in python
import numpy as np
# make an array with numpy
gfg = np.array([1, 2, 3, 4, 5])
# applying ndarray.__invert__() method
print(gfg.__invert__())
输出:
[-2 -3 -4 -5 -6]
示例 #2:
# import the important module in python
import numpy as np
# make an array with numpy
gfg = np.array([[1, 2, 3, 4, 5],
[6, 5, 4, 3, 2]])
# applying ndarray.__invert__() method
print(gfg.__invert__())
输出:
[[-2 -3 -4 -5 -6]
[-7 -6 -5 -4 -3]]