在 Numpy 中将二维数组的数组元素的位右移
要将二维数组的数组元素的位右移,请在 Python Numpy 中使用 **numpy.right_shift()** 方法。位右移 x2 次。因为数字的内部表示形式为二进制格式,所以此操作等效于将 x1 除以 2**x2。
x1 是输入值。x2 是要从 x1 右侧移除的位数。如果 x1.shape != x2.shape,则它们必须可广播到一个公共形状。
函数 right_shift() 返回 x1,其位右移 x2 次。如果 x1 和 x2 都是标量,则这是一个标量。
步骤
首先,导入所需的库 -
import numpy as np
创建一个二维数组 -
arr = np.array([[56, 87, 23], [92, 81, 98]])
显示我们的数组 -
print("Array...",arr)
获取数据类型 -
print("Array datatype...",arr.dtype)
获取数组的维度 -
print("Array Dimensions...",arr.ndim)
获取数组的形状 -
print("Our Array Shape...",arr.shape)
获取数组的元素个数 -
print("Elements in the Array...",arr.size)
右移的计数 -
valRight = 3
要将二维数组的数组元素的位右移,请使用 numpy.right_shift() 方法 -
print("Result (right shift)...",np.right_shift(arr, valRight))
示例
import numpy as np # Create a Two-Dimensional array arr = np.array([[56, 87, 23], [92, 81, 98]]) # Displaying our array print("Array...",arr) # Get the datatype print("Array datatype...",arr.dtype) # Get the dimensions of the Array print("Array Dimensions...",arr.ndim) # Get the shape of the Array print("Our Array Shape...",arr.shape) # Get the number of elements of the Array print("Elements in the Array...",arr.size) # The count of right right valRight = 3 # To shift the bits of an integer to the right, use the numpy.right_shift() method in Python Numpy print("Result (right shift)...",np.right_shift(arr, valRight))
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
Array... [[56 87 23] [92 81 98]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (2, 3) Elements in the Array... 6 Result (right shift)... [[ 7 10 2] [11 10 12]]
广告