python实现判断数组是否包含指定元素的方法

yipeiwu_com6年前Python基础

本文实例讲述了python实现判断数组是否包含指定元素的方法。分享给大家供大家参考。具体如下:

python判断数组是否包含指定的元素的方法,直接使用in即可,python真是简单易懂

print 3 in [1, 2, 3] # membership (1 means true
inventory = ["sword", "armor", "shield", "healing potion"]
if "healing potion" in inventory:
  print "You will live to fight another day."

运行结果如下:

True
You will live to fight another day.

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Django对数据库进行添加与更新的例子

先把models.py摆这儿 #models.py class UserProfile(AbstractUser): ''' 继承Django的AbstractUser 并向里面...

Python 比较两个数组的元素的异同方法

通过set()获取两个数组的交/并/差集: print set(a).intersection(set(b)) # 交集 print set(a).union(set(b)) # 并...

python循环监控远程端口的方法

本文实例讲述了python循环监控远程端口的方法。分享给大家供大家参考。具体如下: 在ip.txt中每行一个ip地址和端口号,代码可循环监控这些ip地址的指定端口是否正常 #!/us...

使用Python脚本操作MongoDB的教程

连接数据库 MongoClient VS Connection class MongoClient(pymongo.common.BaseObject) | Connection...

python 默认参数问题的陷阱

python 里面一个常见的陷阱就是函数的默认参数问题。如下: def func(mylist = []): mylist.append(1) return mylist...