python - 有没有办法在 python 的列表推导中使用两个 if 条件

标签 python list

假设我有一个列表

my_list = ['91 9925479326','18002561245','All the best','good']

现在我想忽略列表中以 9118 开头的字符串,如下所示

result = []
for i in my_list:
   if not '91' in i:
      if not '18' in i:
         result.append(i) 

所以在这里我想通过列表推导来实现这一点。

无论如何要在列表推导中写两个 if 条件吗?

最佳答案

[i for i in my_list if '91' not in i and '18' not in i]

注意你不应该使用 list 作为变量名,它会影响内置函数。

关于python - 有没有办法在 python 的列表推导中使用两个 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11740814/

相关文章:

python - python中根据随机数调用不同的函数

Java使用递归void函数的结果,将每个结果添加到列表中并在另一个函数中返回

performance - Data.Sequence.Seq 与 [] 相比有多快?

algorithm - 从列表中查找一组特定的数字

Python 3 : Crash with free(): corrupted unsorted chunks while reading file

Python 尝试在 PYQT5 中查看 EXR 文件时崩溃

python - 如何使用 matplotlib 从值列表生成直方图?

python - python中是否有一个函数可以将[2011,2012,2013,204]等日期的值映射到2011-2014。其中 [2003,2006,2007,2008] 至 2003, 2006-2008

python - Pandas - 应用于 lambda 函数的列中位数

python - Callable 是无效的基类?