Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
58 views
in Technique[技术] by (71.8m points)

python - How to check if element(b) in list contains an element (a) from the same list. than remove the element (a)

I need to remove an element if it's in another element

    def c_counter_check(new_filters):         #new filters is an list
        for i in new_filters_splited[:]:
            index = new_filters_splited.index(i) #get index of an i
            mylist = new_filters_splited[:index] + new_filters_splited[index + 1:] # create a compare list without i
            for a in mylist[:]:
                if a.__contains__(i):    #check if an element from mylist contains an i
                    new_filters_splited.remove(i)      #if yes than remove i from new_filters_splited
        return new_filters_splited

If for example my list is ['a', 'ab', 'ac', 'ab', 'abac'] I need to remove 'a' and 'ab' and 'abac' and 'ac', in result should be just ['a']

question from:https://stackoverflow.com/questions/65893382/how-to-check-if-elementb-in-list-contains-an-element-a-from-the-same-list-t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Remove only first occurrence of lookup value from input_list

If we want to remove only the first occurrence of an item from the lookup, then we need to maintain the lookup list. The first time an item is found in the lookup list, we exclude it from the output_list. In addition, we should also remove that item from the lookup list. That way if there are other occurrences of the same value, it can be populated into the output_list.

To do this, the filter_list function has to be modified a bit.

The updated version of the filter_list function will be:

def filter_list (input_list, lookup_items):
    
    output_list = []               #initialize the output_list
    
    for a in input_list:           #iterate thru the input_list
        if a in lookup_items:      #check if value in input_list is part of lookup_items
            lookup_items.remove(a) #remove it from lookup_items and don't append to output_list
        else:
            output_list.append(a)  #append to output list
    
    return output_list             #return the output_list that meets our criteria

Now let's test it out:

print ("Output List  : ", filter_list(['a', 'ab', 'ac', 'ab'], ['a','ab']))

Output of this will be:

Output List  :  ['ac', 'ab']

As you can see, the second occurrence of ab is retained and passed back.

Another test:

print ("Output List  : ", filter_list(['x', 'xy', 'xyz', 'yz'], ['x','yz']))

Output of this will be:

Output List  :  ['xy', 'xyz']

New Answer using a function call with arguments

If you are not sure what the input list is and the lookup items are, then you can define a function and call the function with both input_list and lookup_list. Assume you will have these two values before you need to do the filter operation.

In the below listed function, we are going to send the input_list and lookup_list. This will then filter out all elements in the lookup_list and return only the ones that are not in the list:

def filter_list (input_list, lookup_items):

    return [a for a in input_list if a not in lookup_items]

Let's test this out.

print ("Output List  : ", filter_list(['a', 'ab', 'ac', 'ab'], ['a','ab']))

For this, I am sending the following data:

print ("Input List   : ['a', 'ab', 'ac', 'ab']")
print ("Lookup Items : ['a','ab']")

Input List   : ['a', 'ab', 'ac', 'ab']
Lookup Items : ['a','ab']

The output should be ['ac']

As expected, the output is:

Output List  :  ['ac']

Let's run another test:

print ("Output List  : ", filter_list(['x', 'xy', 'xyz', 'yz'], ['x','yz']))

The output as expected is:

Output List  :  ['xy', 'xyz']

Previous Answer to exclude items from a list

Here's how to remove all the items using a lookup.

input_list = ['a', 'ab', 'ac', 'ab']
lookup_items = ['a', 'ab']

output_list = [a for a in input_list if a not in lookup_items]

print ('Input List   : ', input_list)
print ('Lookup Items : ', lookup_items)
print ('Output List  : ', output_list)

This is a list comprehension. The expanded code is shown below:

#output_list = [a for a in input_list if a not in lookup_items]

#The above line is a list comprehension. It can be translated to a for loop as follows:

output_list = []
for a in input_list:
    if a not in lookup_items:
        output_list.append(a)

Here, I am removing any item that is in the lookup_items list. Once you clarify if only the first item needs to be removed, then I can modify the code to show that.

The output of this will be:

Input List   :  ['a', 'ab', 'ac', 'ab']
Lookup Items :  ['a', 'ab']
Output List  :  ['ac']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...