簡體   English   中英

在列表中查找在python中具有公差的特定范圍內的值?

[英]Find values in list which lies within certain range with tolerance in python?

我試圖找到位於某個范圍內的某個值。 我知道使用 np.isclose 會以一定的容忍度做到這一點。 但是,它將根據布爾變量打印出結果。 所以,我想做同樣的事情,但只打印出值而不是 True 或 False。

mylist = (1.5,3.1251,5.8741,9.213,7.858,2.1242,8.18956,2.5452,4.745,2.1254)
threshold = 3.5
result = np.isclose(mylist, threshold, rtol = 1e-05)
print(result) 

而不是下面的打印:

result = array([False, True, False, False, False, True, False, True, False, True])

我希望它打印以下內容:

result = array([3.1251, 2.1242, 2.5452, 2.1254])

PS 結果只是一個例子,不是真正的結果。

編輯 1

我設法將代碼更改為以下內容:

def check_x_axis_list(comp_list, target_value, tolerance):
    x_axis_result = []
    for x in range(0, len(comp_list)):
        curent_value = comp_list[x]
        if curent_value >= target_value - tolerance and curent_value <= target_value + tolerance:
            x_axis_result.append(comp_list[x])
    return x_axis_result

對於寬容,我嘗試執行以下操作:

tol_max = max(mylist)
tol_min = min(mylist)
tol = (tol_max - tol_min) / 2

但是,我一直有這個錯誤:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

編輯 2

我將代碼更改為以下代碼,但仍然出現相同的錯誤

result = [val for val in comp_list if abs(val - target_value) < tolerance]
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

編輯 3

方程

帶峰的 PSD 圖

方程繪圖參數

我試圖從 PSD 圖中獲取值 w1 和 w2,上面的代碼是一種消除其他值的方法,其中目標值是帶有星號標記的 x 軸峰值,並搜索接近 x-軸值具有一定的公差。 我希望代碼進行搜索,而不是給我布爾結果,而是給我接近峰值的值。 交點值低於峰值位置。

Amplitude values:  [0.0004046159973339667, 0.0003064079718686719]
Current Amplitude value:  0.0004046159973339667
Current half power amplitude value:  0.00028610671549140587
Current Amplitude value:  0.0003064079718686719
Current half power amplitude value:  0.00021666315471795475

Intersection Values:  [array([11.6705359 , 13.66919925, 21.84434139, 22.53181091, 27.88789357,28.17911233]), array([11.43294083, 14.12791966, 21.28003529, 23.43686901, 27.50441635,28.79179351])]

編輯 4

我以下代碼有效:

def check_x_axis_list(comp_list, target_value, tolerance):
    x_axis_result = []
    for x in range(0, len(comp_list)):
        if comp_list[x] >= (target_value - tolerance) and comp_list[x] <= (target_value + tolerance):
            x_axis_result.append(comp_list[x])
    return x_axis_result

def check_x_axis_list(comp_list, target_value, tolerance):
    x_axis_result = [val for val in comp_list if abs(val - target_value) < tolerance]
    return x_axis_result

但是,我唯一的困難是如何操作公差值以獲得接近 x 軸幅度值的值,因為當它達到 28 時,它會打印出第二個峰值交點和第三個峰值交點,而不僅僅是第三個高峰路口。 請參閱以下問題:

Intersection: [11.6705359 , 13.66919925, 21.84434139, 22.53181091, 27.88789357, 28.17911233]
Current x-axis Peak Amplitude value:  13.0
28.17911232801107
11.670535903774892
8.254288212118087
[11.670535903774892, 13.66919924780022]
Intersection: 11.43294083, 14.12791966, 21.28003529, 23.43686901, 27.50441635, 28.79179351]
Current x-axis Peak Amplitude value:  28.0
28.791793514060206
11.432940831732218
8.679426341163994
[21.280035294406446, 23.436869009131495, 27.504416349364988, 28.791793514060206]

你可以試試這個:

print(np.array(mylist)[result])

輸出:

 array([3.1251, 2.1242, 2.5452, 2.1254])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM