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

相关文章

Python谱减法语音降噪实例

代码中用到了nextpow2,其中n = nextpow2(x) 表示最接近x的2的n次幂。 #!/usr/bin/env python import numpy as np imp...

Python中使用Counter进行字典创建以及key数量统计的方法

这里的Counter是指collections中的Counter,通过Counter可以实现字典的创建以及字典key出现频次的统计。然而,使用的时候还是有一点需要注意的小事项。 使用Co...

python 实现提取某个索引中某个时间段的数据方法

如下所示: from elasticsearch import Elasticsearch import datetime import time import dateutil.p...

使用python实现strcmp函数功能示例

实现这个功能我相信大家一定明白他的意思了,很简单了,下面的代码大家参考使用吧复制代码 代码如下:def strcmp(str1,str2):    ...

在Python中操作时间之tzset()方法的使用教程

 tzset()方法重置所使用的库例程的时间转换规则。环境变量TZ指定如何完成此操作。 TZ环境变量的标准格式(空格为清楚起见而加的): 复制代码 代码如下:std offse...