您好,欢迎访问一九零五行业门户网

Python 检查数组元素是否存在 类PHP isset()

php中有isset方法来检查数组元素是否存在,在python中无对应函数。 python的编程理念是“包容错误”而不是“严格检查”。举例如下: look before you leap (lbyl): if idx len(array): array[idx]else: #handle this easier to ask forgiveness than permiss
php中有isset方法来检查数组元素是否存在,在python中无对应函数。
python的编程理念是“包容错误”而不是“严格检查”。举例如下:
look before you leap (lbyl):
if idx
easier to ask forgiveness than permission (eafp):
try: array[idx]except indexerror: #handle this
所以在python中一般可以通过异常来处理数组元素不存在的情况,而无须事先检查。如果不希望看见异常处理,也可以像下面这样:
if 'test' in ['demo','example']: ...else: ...
by iefreer
其它类似信息

推荐信息