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.7 dataclass使用指南小结

dataclass简介 dataclass的定义位于PEP-557,根据定义一个dataclass是指“一个带有默认值的可变的namedtuple”,广义的定义就是有一个类,它的属性均可...

python 实现矩阵按对角线打印

python 实现矩阵按对角线打印

如下所示: Description: 将一个矩阵(二维数组)按对角线向右进行打印。(搜了一下发现好像是美团某次面试要求半小时手撕的题) Example: Input: [ [1,2,...

Python中enumerate()函数编写更Pythonic的循环

enumerate函数 enumerate是一个Python内置函数,一个功能强大的内置函数。其实功能强大不足以形容它, 但是很难用一个词来形容它的用途。 让我们来看看一个使用enum...

pandas实现选取特定索引的行

如下所示: >>> import numpy as np >>> import pandas as pd >>> index=n...

Python中sort和sorted函数代码解析

本文研究的主要是Python中sort和sorted函数的相关内容,具体如下。 一、sort函数 sort函数是序列的内部函数 函数原型: L.sort(cmp=None, key=No...