蟒蛇 - 如果不在列表中 [英] python - if not in list

查看:135
本文介绍了蟒蛇 - 如果不在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:

$ $ p $ mylist = ['total','age','gender','region' ,'sex']
checklist = ['total','civic']

I必须使用我继承的一些代码,如下所示:

 对于mylist中的项目:
if item in清单:
做一些事情:

如何使用上面的代码来告诉我公民不在mylist



这将是理想的方式,但我不能使用它,不问我为什么。

 清单中的项目:
如果项目不在mylist中:
打印项目

结果:

  civic 


解决方案

这个怎么样?
$ b

 用于mylist中的项目:
如果项目在清单中:
传递
else:

打印项目


I have two lists:

mylist = ['total','age','gender','region','sex']
checklist = ['total','civic']

I have to work with some code I have inherited which looks like this:

for item in mylist:
    if item in checklist:
        do something:

How can I work with the code above to tell me that 'civic' is not in mylist?.

This would've been the ideal way to do it but I cant use it, don't ask me why.

for item in checklist:
    if item not in mylist:
        print item

Outcome:

civic

解决方案

How about this?

for item in mylist:
    if item in checklist:
        pass
    else:
       # do something
       print item

这篇关于蟒蛇 - 如果不在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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