我想从列表中删除一个元素,以便该元素包含'X'
或'N'
。我必须申请一个大型基因组。这是一个例子:
输入:
codon=['AAT','XAC','ANT','TTA']
预期输出:
codon=['AAT','TTA']
最佳答案
出于基本目的
>>> [x for x in ['AAT','XAC','ANT','TTA'] if "X" not in x and "N" not in x]
['AAT', 'TTA']
但是,如果您有大量数据,建议您使用dict或set
而且,如果您有X和N以外的许多字符,您可能会这样做
>>> [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']
注意:
list(x)
可以只是x
,而["X","N","Y","Z","K","J"]
可以只是"XNYZKJ"
,请引用gnibbler的答案,他做到了最好。