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, 45, 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 One-Dimensional array
arr = np.array([56, 87, 23, 92, 81, 98, 45, 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 45 98] Array datatype... int64 Array Dimensions... 1 Our Array Shape... (8,) Elements in the Array... 8 Result (left shift)... [448 696 184 736 648 784 360 784]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP