Home > Blockchain >  implement the filter function to remove certain elements from a list
implement the filter function to remove certain elements from a list

Time:10-26

I have the following function whose function is to return a list with the indices of the neighbors of each element in a list.

example: in the following list l = [1, 2, 3, 4, 5] the indices of the neighbors of the element in position 0 would be 1, the indices of the neighbors of the element in position 1 would be 0 and 2 and so on...In addition, the list of indices includes the index of the element itself.

Implement the function as follows:

 """Returns the list of neighbor indices. It is included in the element itself """
    
    indices = []
    
    if index == 0:
        # first
        indices.append(index   1)
    elif index == len(elements) - 1:
        # latest
        indices.append(index - 1)
    else:
        indices.append(index   1)
        indices.append(index - 1)
    
    # include the element itself as a neighbor of itself
    indices.append(index)
    
    return indices

However now I want to implement the function using filter to remove the conditionals, however it doesn't work for me the way I wrote it and it's hard to see the error...

def get_neighbour_indices(index, elements):
     """Returns the list of neighbor indices. It is included in the element itself """
    
     indices = []
    
     indices.append(index   1)
     indices.append(index - 1)
   
     # include the element itself as a neighbor of itself
     indices.append(index)
    
     # remove impossible indices (less than zero and greater than or equal to the length of the list)
     indices_delete = list(filter(lambda index : (indices[index]) < 0 and (indices[index]) >= len(elements), indices))
     for index in indices:
         indices.remove(indices_delete)
    
     return indices

I try to implement the filter function in my function to remove the conditionals but the code doesn't work

CodePudding user response:

As to your question: your filtering function probably fails because

  • you used "and" instead of "or".

  • The removal part:

     for index in indices:
        indices.remove(indices_delete)
    

    should be

     for index in indices_delete:
        indices.remove(index)
    

So either fix the filtering part, or simply replace it with:

indices=[ i in indices if 0 <= i < len(elements)]

(instead of finding illegal indices then removing them, directly filter the legal ones :) ).

CodePudding user response:

If I understand your question correctly, you are looking to find the neighbours of a specific index in a list. I would try using list slicing to filter a list to a range of values.

This function should do the trick:

def get_neighbour_indices(index, elements):
    if (index < len(elements) - 1) and (index - 1 > 0):
        return elements[index - 1:index   2]
    if index == 0:
        return elements[:index   2]
    if index == len(elements) - 1:
        return elements[-2:]
  • Related