2017-05-03 301 views
-2

我有一個向量數組是pt = [83。 0.131 0.178 0.179 0.227 0. 0] 所以我想將這些值相互比較,並刪除所有在+5範圍內的值。例如,在這個數組中,我想刪除值179,因爲它在值178 + -5的範圍內。 我想這Python - 如何刪除特定範圍內的數組中的某些值?

for i in pt_list: 
position = [i[0] for i in pt_list] 
counter1 += 1 
if(counter1 > 1): 
    if not position in range (prior_x0 - 5, prior_x0 +6): 
     arr = np.array([[position, 0]]) 
     pt_list = np.append(later_pt_list, later_arr, axis = 0) 
prior_x0 = position 
a = pt_list[np.argsort(later_pt_list[:,0])] 

打印的(a)

和結果仍然是相同的數組:|

+0

你有什麼結果?任何代碼示例? –

+0

向你的問題添加代碼 –

回答

0

是您需要的嗎?我已經添加了一小部分來照顧您輸入數據的格式。我認爲這是一個帶有矢量的文本列表。如果沒有,你可以相應地改變它。 我有一個理解列表的版本,但它很難讀。 輸出是'list_float'。 我以爲你要記住這是在其他的範圍內的第一載體,並刪除以下

# Make sure the format of your input is correct 
list = ['83. 0.', '131. 0.', '178. 0.', '179. 0.', '227. 0.'] 
list_float = [] 
for point in list: 
    head, _, _ = point.partition(' ') 
    list_float.append(float(head)) 

# This is the bit removing the extra part 
for pos, point in enumerate(list_float): 
    for elem in list_float[pos+1:]: 
     if (elem < point+5.) and (elem > point-5.): 
      list_float.remove(elem) 

print(list_float) 
+0

這就是我想要的,非常感謝你:D –

+0

歡迎你。注意它作爲解決方案:) – RysDe

0

這就是你想要的嗎?

pt_list = [83.0, 131.0, 178.0, 179.0, 227.0] 
def removeNumbers(value,ran): 
    return [x for x in pt_list if x not in (range(value + 1, value + (ran+1)) + range(value - ran, value))] 
print removeNumbers(178,5) 
相關問題