有没有一种方法可以在python中的列表推导中使用两个if条件 [英] Is there a way to use two if conditions in list comprehensions in python

查看:134
本文介绍了有没有一种方法可以在python中的列表推导中使用两个if条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个列表

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

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

Now I want to ignore the strings in the list starting with 91 and 18 like below

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

所以在这里我想通过列表理解来实现.

So here I want to achieve this with list comprehensions.

无论如何,列表推导中是否要写两个if条件?

Is there anyway to write two if conditions in list compreshensions?

推荐答案

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

请注意,您不应使用list作为变量名,它会掩盖内置函数.

Note you shouldn't use list as a variable name, it shadows the built-in function.

这篇关于有没有一种方法可以在python中的列表推导中使用两个if条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆