如何检查列表索引是否存在 [英] How to check whether a list index is present or not

查看:93
本文介绍了如何检查列表索引是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下列表

a = ['a','c','d']

我需要检查列表索引4(或任何索引)是否存在于列表中.是否有类似于python中的php isset 的方法(不使用异常处理) )?

I need to check whether the list index 4(or any index) is present on the list or not. Is there a method similar to php isset in python for this(without using exception handling)?

推荐答案

您可以使用len()检查索引是否存在.

You can use len() to check whether the Index is present or not.

由于len(list)返回类似last_index+1的内容:

In [18]: a = ['a','c','d']

In [19]: len(a)-1 > 4  #or len(a)>4
Out[19]: False

In [20]: len(a)-1 > 2
Out[20]: True

这篇关于如何检查列表索引是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆