返回 NumPy 中特定列表元素的尾數和指數對


要返回給定列表的尾數和指數對,請在 Python NumPy 中使用**numpy.frexp()**方法中的索引值。輸出是一個儲存結果的位置。如果提供,則其形狀必須與輸入廣播的形狀相同。如果沒有提供或為 None,則返回一個新分配的陣列。元組(僅作為關鍵字引數可能)的長度必須等於輸出的數量。

條件在輸入上進行廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他位置,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。

步驟

首先,匯入所需的庫:

import numpy as np

建立一個列表:

myList = [55.8, 45.5, 166.8, -14.8, 78,6, -19.8]

顯示列表:

print("List...
", myList)

列表的長度:

print("
List length...
", len(myList))

列表的型別:

print("
List type...
", type(myList))

返回給定列表的尾數和指數對:

print("
Return mantissa and exponent as a pair of a given list...
",np.frexp(myList))

要返回給定列表的尾數和指數對,請在 Python NumPy 中使用 numpy.frexp() 方法中的索引值:

print("
Result (specific element)...
",np.frexp(myList[3]))

示例

import numpy as np

# Create a list
myList = [55.8, 45.5, 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)) # Return mantissa and exponent as a pair of a given list print("
Return mantissa and exponent as a pair of a given list...
",np.frexp(myList)) # To return mantissa and exponent as a pair of a given list, use the index value in the numpy.frexp() method in Python Numpy print("
Result (specific element)...
",np.frexp(myList[3]))

輸出

List...
[55.8, 45.5, 166.8, -14.8, 78, 6, -19.8]

List length...
7

List type...
<class 'list'>

Return mantissa and exponent as a pair of a given list...
(array([ 0.871875 , 0.7109375, 0.6515625, -0.925 , 0.609375 , 0.75 , -0.61875 ]), array([6, 6, 8, 4, 7, 3, 5], dtype=int32))

Result (specific element)...
(-0.925, 4)

更新於:2022年2月8日

95 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告