How to remove specific element from an array using python

后端 未结 6 971
不知归路
不知归路 2020-12-12 17:57

I want to write something that removes a specific element from an array. I know that I have to for loop through the array to find the element that matches the c

6条回答
  •  再見小時候
    2020-12-12 18:16

    There is an alternative solution to this problem which also deals with duplicate matches.

    We start with 2 lists of equal length: emails, otherarray. The objective is to remove items from both lists for each index i where emails[i] == 'something@something.com'.

    This can be achieved using a list comprehension and then splitting via zip:

    emails = ['abc@def.com', 'something@something.com', 'ghi@jkl.com']
    otherarray = ['some', 'other', 'details']
    
    from operator import itemgetter
    
    res = [(i, j) for i, j in zip(emails, otherarray) if i!= 'something@something.com']
    emails, otherarray = map(list, map(itemgetter(0, 1), zip(*res)))
    
    print(emails)      # ['abc@def.com', 'ghi@jkl.com']
    print(otherarray)  # ['some', 'details']
    

提交回复
热议问题