NumPy中二维数组元素的位左移
要将二维数组元素的位向左移动,请在Python NumPy中使用**numpy.left_shift()**方法。位通过在x1的右侧附加x2个0来向左移动。由于数字的内部表示形式为二进制格式,因此此操作等效于将x1乘以2**x2。x1是输入值。x2是要附加到x1的零的个数。必须是非负数。如果x1.shape != x2.shape,则它们必须可广播到一个公共形状(这将成为输出的形状)。
left_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)左移的次数:
valLeft = 3
要将二维数组元素的位向左移动,请在Python NumPy中使用numpy.left_shift()方法:
print("
Result (left shift)...
",np.left_shift(arr, valLeft))示例
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 left shift
valLeft = 3
# To shift the bits of an integer to the left, use the numpy.left_shift() method in Python Numpy
print("
Result (left shift)...
",np.left_shift(arr, valLeft))输出
Array... [[56 87 23] [92 81 98]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (2, 3) Elements in the Array... 6 Result (left shift)... [[448 696 184] [736 648 784]]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP