2017-05-03 136 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) 
相关问题