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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

Python3读取UTF-8文件及统计文件行数的方法

本文实例讲述了Python3读取UTF-8文件及统计文件行数的方法。分享给大家供大家参考。具体实现方法如下: ''''' Created on Dec 21, 2012 Pyth...

详解Python字符串切片

详解Python字符串切片

在python中,我们定义好一个字符串,如下所示。 在python中定义个字符串然后把它赋值给一个变量。 我们可以通过下标访问单个的字符,跟所有的语言一样,下标从0开始(==,我自己...

Python Pillow.Image 图像保存和参数选择方式

保存时代码如下: figure_corp = figure.crop( (32*rate/2, 32*rate/2, 32-32*rate/2, 32-32*rate/2)) fig...

python实现自主查询实时天气

python实现自主查询实时天气

本文实例为大家分享了python实现自主查询实时天气的具体代码,供大家参考,具体内容如下 用到了urllib2 json  很简单的一个应用 如下 获取城市编号 #cod...

Python中os.path用法分析

本文实例分析了Python中os.path用法。分享给大家供大家参考。具体如下: 复制代码 代码如下:#coding=utf-8 import os print os.path.absp...