过滤列表。仅在项目之间有一定距离的情况下获取列表元素?

Filtering a list. Get elements of list only with a certain distance between items?

我只需要获取那些 elements/items 在某种程度上彼此相距较远的。例如,我有一个整数列表:

data = [-2000, 1000, 2000, 3500, 3800, 4500, 4600, 5000, 6000]

假设我只想检索彼此之间距离至少为 1000 的那些元素。 从上面的列表中我需要输出:

[-2000, 1000, 2000, 3500, 4500, 6000]

我是这样过滤的:

filtered.append(data[0])
for index, obj in enumerate(data):
    if index < (l - 1): 
        if abs(obj - data[index+1]) > 999:
            filtered.append(data[index+1])

print(filtered)

不需要的输出:

[-2000, 1000, 2000, 3500, 6000]

它失败了,因为它比较了两个相邻的列表元素,尽管有些元素应该被过滤掉,不应该被考虑在内。

让我展示的更清楚
原始列表:[-2000, 1000, 2000, 3500, 3800, 4500, 4600, 5000, 6000]

过滤过程:

-2000 - OK
1000 - OK
2000 - OK
3500 - OK
3800 - Out
4500 - Should be OK, but it filtered out compared to 3800. But it should be compared to 3500 (not 3800 which is Out). 

如何解决?

对数据进行排序,然后与之前的数据进行比较即可:

data = [-2000, 1000, 2000, 3500, 3800, 4500, 4600, 5000, 6000]

lst = [min(data)]          # the min of data goes in solution list
for i in sorted(data[1:]):
  if i-lst[-1] > 999:      # comparing with last element
    lst.append(i)          # of the solution list

print (lst)

输出:

[-2000, 1000, 2000, 3500, 4500, 6000]