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中将字典转换为XML以及相关的命名空间解析

尽管 xml.etree.ElementTree 库通常用来做解析工作,其实它也可以创建XML文档。 例如,考虑如下这个函数: from xml.etree.ElementTree...

Linux下为不同版本python安装第三方库

问题背景 目前的linux发行版上,有很多安装了两个版本的python。 我的机器上默认的版本为python 2.x。且在使用easy_install安装第三方库时,也默认安装到了2...

python通过paramiko复制远程文件及文件目录到本地

最近写运维自动化平台,需要用python写很多的小功能模块。 这里就分享一个用Python的paramiko来实现功能的一段代码: 复制远程服务器上的文件及文件夹到本地目录。 解释一下什...

Python多线程处理实例详解【单进程/多进程】

Python多线程处理实例详解【单进程/多进程】

本文实例讲述了Python多线程处理操作。分享给大家供大家参考,具体如下: python — 多线程处理 1、一个进程执行完后,继续下一个进程 root@72132server:~#...

python查找第k小元素代码分享

复制代码 代码如下:# -*- coding: utf-8 -*- from random import randintfrom math import ceil, floor def...