在 NumPy 中返回給定列表的尾數和指數對
要返回給定列表的尾數和指數對,請在 Python NumPy 中使用 **numpy.frexp()** 方法。輸出是一個儲存結果的位置。如果提供,它必須具有輸入廣播到的形狀。如果不提供或為 None,則返回一個新分配的陣列。元組(僅作為關鍵字引數可能)的長度必須等於輸出的數量。
條件在輸入上進行廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立了一個未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化。
步驟
首先,匯入所需的庫 -
import numpy as np
建立一個列表 -
myList = [15.9, 39.2, 166.8, -14.8, 78,6, -19.8]
顯示陣列 -
print("List...
", myList)
列表的長度 -
print("
List length...
", len(myList))
列表的型別 -
print("
List type...
", type(myList))
要返回給定列表的尾數和指數對,請在 Python NumPy 中使用 numpy.frexp() 方法 -
print("
Result...
",np.frexp(myList))
示例
import numpy as np # Create a list myList = [15.9, 39.2, 166.8, -14.8, 78,6, -19.8] # Display the list print("List...
", myList) # Length of the list print("
List length...
", len(myList)) # Type of the list print("
List type...
", type(myList)) # To return mantissa and exponent as a pair of a given list, use the numpy.frexp() method in Python Numpy print("
Result...
",np.frexp(myList))
輸出
List... [15.9, 39.2, 166.8, -14.8, 78, 6, -19.8] List length... 7 List type... <class 'list'> Result... (array([ 0.99375 , 0.6125 , 0.6515625, -0.925 , 0.609375 , 0.75 , -0.61875 ]), array([4, 6, 8, 4, 7, 3, 5], dtype=int32))
廣告