Removing an element from a list based on a predicate

前端 未结 9 1266
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 12:36

I want to remove an element from list, such that the element contains \'X\' or \'N\'. I have to apply for a large genome. Here is an example:

相关标签:
9条回答
  • 2021-01-18 13:18
    1. filter(lambda x: 'N' not in x or 'X' not in x, your_list)
    2. your_list = [x for x in your_list if 'N' not in x or 'X' not in x]
    0 讨论(0)
  • 2021-01-18 13:18

    I like gnibbler’s memoization approach a lot. Either method using memoization should be identically fast in the big picture on large data sets, as the memo dictionary should quickly be filled and the actual test should be rarely performed. With this in mind, we should be able to improve the performance even more for large data sets. (This comes at some cost for very small ones, but who cares about those?) The following code only has to look up an item in the memo dict once when it is present, instead of twice (once to determine membership, another to extract the value).

    codon = ['AAT', 'XAC', 'ANT', 'TTA']
    def pred(s,memo={}):
        try:
            return memo[s]
        except KeyError:
            memo[s] = not any(y in s for y in "XN")
        return memo[s]
    
    filtered = filter(pred, codon)
    

    As I said, this should be noticeably faster when the genome is large (or at least not extremely small).

    If you don’t want to duplicate the list, but just iterate over the filtered list, do something like:

    for item in (item for item in codon if pred):
        do_something(item)
    
    0 讨论(0)
  • 2021-01-18 13:22

    There is also the method of doing it using filter

        lst = filter(lambda x: 'X' not in x and 'N' not in x, list)
    
    0 讨论(0)
提交回复
热议问题