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

相关文章

Python QQBot库的QQ聊天机器人

Python QQBot库的QQ聊天机器人

本文实例为大家分享了Python QQBot库的QQ聊天机器人的具体代码,供大家参考,具体内容如下 项目地址:https://github.com/pandolia/qqbot 1.安装...

python设置值及NaN值处理方法

如下所示: python 设置值 import pandas as pd import numpy as np dates = pd.date_range('20180101',pe...

Python控制Firefox方法总结

Python控制Firefox方法总结

有时候为了自动化测试网页,我们往往希望能够使用一些脚本语言控制浏览器. 通过脚本模拟一些浏览器动作,然后测试得到的结果.这里, 我们讲解一下如何使用Python语言控制Firefox浏览...

Python编程实现蚁群算法详解

Python编程实现蚁群算法详解

简介 蚁群算法(ant colony optimization, ACO),又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型算法。它由Marco Dorigo于1992年在他的博士论文...

浅谈numpy数组中冒号和负号的含义

在实际使用numpy时,我们常常会使用numpy数组的-1维度和”:”用以调用numpy数组中的元素。也经常因为数组的维度而感到困惑。 总体来说,”:”用以表示当前维度的所有子模块 “-...