列表索引() [英] list index()

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

问题描述

发现异常的列表的index()函数是什么?
找到了什么?让我们希望这在Python 3中得到纠正。如果不出意外,请添加

一个不会抛出异常的函数。有一百万个

的情况,你可以让一个项目不在列表中而且不是一个例外的情况。


什么'使用list()函数的列表抛出异常而不是找到
?让我们希望这在Python 3中得到纠正。如果不出意外,请添加

一个不会抛出异常的函数。有一百万

的情况,你可以让一个项目不在列表中而且不是一个例外的情况。



自己编写这样的函数,毕竟很容易。我很少使用`list.index()`方法
。你的数百万的情况是什么样的?

也许有比这些情况更好的数据结构!?


Ciao,

Marc''BlackJack''Rintsch


zz ***** *@aol.com 写道:


什么与list的index()函数有关,而不是在$ b上抛出异常

找到了吗?



它让你知道该项目不在列表中。从指数中没有

合理的回报值。在这种情况下运行。


让我们希望在Python 3中纠正这个问题。如果没有别的,请添加一个

函数不要抛出异常。



您可以轻松创建一个:


def get_an_index_even_if_not_found(the_list,the_item):

bogus_index_value = object()

试试:

index = the_list.index(the_value)

除了ValueError:

index = bogus_index_value

返回索引


这取决于你想要使用什么bogus_index_value

。我们其他人将继续在需要的地方发现例外。


-

\Reichel'定律:度假的机构趋向于继续休假|

` \除非被外部势力采取行动。 - Carol Reichel |
_o__)|

Ben Finney


2007年8月30日星期四17:09 :36 + 1000,Ben Finney写道:

zz ****** @ aol .com 写道:


>列表的index()函数是什么?抛出异常未找到?



它让你知道该项目不在列表中。从指数中没有

合理的回报值。在那种情况下运作。



-1怎么样? C程序员一直这样做。 :-)


Ciao,

Marc''BlackJack''Rintsch


What''s with the index() function of lists throwing an exception on not
found? Let''s hope this is rectified in Python 3. If nothing else, add
a function that doesn''t throw an exception. There are a million
situations where you can have an item not be in a list and it is not
an exception situation.

解决方案

On Wed, 29 Aug 2007 23:44:33 -0700, zzbbaadd wrote:

What''s with the index() function of lists throwing an exception on not
found? Let''s hope this is rectified in Python 3. If nothing else, add
a function that doesn''t throw an exception. There are a million
situations where you can have an item not be in a list and it is not
an exception situation.

Write such a function yourself, it is quite easy after all. I very seldom
use the `list.index()` method. What do your millions situations look like?
Maybe there is a better data structure than lists for those situations!?

Ciao,
Marc ''BlackJack'' Rintsch


zz******@aol.com writes:

What''s with the index() function of lists throwing an exception on not
found?

It''s letting you know that the item isn''t in the list. There''s no
sensible return value from an "index" function in that condition.

Let''s hope this is rectified in Python 3. If nothing else, add a
function that doesn''t throw an exception.

You can easily create one:

def get_an_index_even_if_not_found(the_list, the_item):
bogus_index_value = object()
try:
index = the_list.index(the_value)
except ValueError:
index = bogus_index_value
return index

It''s up to you to figure out what bogus_index_value you want to
use. The rest of us will continue to catch the exception where needed.

--
\ "Reichel''s Law: A body on vacation tends to remain on vacation |
`\ unless acted upon by an outside force." -- Carol Reichel |
_o__) |
Ben Finney


On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:

zz******@aol.com writes:

>What''s with the index() function of lists throwing an exception on not
found?


It''s letting you know that the item isn''t in the list. There''s no
sensible return value from an "index" function in that condition.

What about -1? C programmers do this all the time. :-)

Ciao,
Marc ''BlackJack'' Rintsch


这篇关于列表索引()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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