Removing an element from a list based on a predicate

前端 未结 9 1281
伪装坚强ぢ
伪装坚强ぢ 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:05

    For basis purpose

    >>> [x for x in ['AAT','XAC','ANT','TTA'] if "X" not in x and "N" not in x]
    ['AAT', 'TTA']
    

    But if you have huge amount of data, I suggest you to use dict or set

    And If you have many characters other than X and N, you may do like this

    >>> [x for x in ['AAT','XAC','ANT','TTA'] if not any(ch for ch in list(x) if ch in ["X","N","Y","Z","K","J"])]
    ['AAT', 'TTA']
    

    NOTE: list(x) can be just x, and ["X","N","Y","Z","K","J"] can be just "XNYZKJ", and refer gnibbler answer, He did the best one.

提交回复
热议问题