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

Erba Aitbayev

I need to get only those elements/items that are to some extent distant from each other. For example, I have a list of integers:

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

Let's assume I want to retrieve only those elements that have have a distance of at least 1000 between each other. From the list above I need output:

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

I'm filtering this way:

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)

Undesired output:

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

It fails because it compares two adjacent list elements, irregardless of the fact that some elements supposed to be filtered out and should not be taken into account.

Let me show more clearly.
Original list: [-2000, 1000, 2000, 3500, 3800, 4500, 4600, 5000, 6000]

Filtering process:

-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). 

How to fix it?

lycuid

Sorting the data and then comparing to the previous would do it:

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)

ouput:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Minimum distance between two elements of a circular list?

keeping only elements in a list at a certain distance at least - changing iterator while looping - Python

How to get only the first elements from each items of a list

Certain number of items in a list

List comprehension and filtering elements

Replacing certain elements in a list

Filtering a list of elements by a list of filters

Get common list elements between list and a nested list in python

Filtering a List with an array items

Filtering a list, removing all items but "like" items

Generator that ensures minimum distance between two items in a list

Display only certain list items in listview with javascript

Find distance between list's elements

Filtering list of items with JQuery checkboxes

Get n elements of a list, each of them at the same distance from the other

Python: Average distance between string elements in a list

Filter list: get only unique elements in objects

Filtering list items with case insensitive

how do you replace only a certain number of items in a list randomly?

Using foreach to show only certain elements in a list

Calculating Distance Between Two Items in a List

Get list of strings between certain strings in bash

AutoComplete not filtering the list items Angular

Trying To Get List To Only List Items With Matching ClientId

segmentation of elements of a list into sub-groups according to the distance between it

How to get the Items between two certain Symbols in a List

How to create whitespace between certain list elements?

Get only the users that contain a certain list column

From a list, how to get only the items with dates within a certain time period?