检查对象是否在列表中 [英] checking if an object IS in a list

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

问题描述




我想测试一个对象是否在列表中(身份而不是平等

测试)。

我可以,如果当然写这样的东西:


test = False

myobject = MyCustomClass(* args,** kw)

为mylist中的元素:

如果元素是myobject:

test = True

break


我甚至可以写一个isinlist(elt,mylist)函数。


但是大多数时候,当我需要python的一些基本功能时,我

后来发现它实际上已经实现了。 ;-)


所以,在python中已经有类似的东西吗?

我试着写:

''元素是在mylist''

但这似乎是不正确的语法...


所有涉及的对象都有一个''__eq__''方法。 />
谢谢,


NP


PS:顺便说一下,如何实现设置元素比较?我的第一个

印象是'a'和'b''成员被认为是相等的如果和

只有当hash(a)== hash(b) ,但我显然错了:


>> class A(object):



.... def __eq __(self,y):

.... return False

.... def __hash __(自我):

....返回5

....


>> a = A(); b = A()
a == b



False


>> hash(b)== hash(a)



True


>> b in set([a])



错误


>> S = set([a])
S.difference([b])



set([< __ main __。对象位于0xb7a91dac>])


所以也有一些平等检查,也许只有当''__eq__''实现了

解决方案

ni ************** *@gmail.com 写道:





我想测试对象是否在一个列表(身份而不是平等

测试)。

我可以写一下这样的东西:


test = False

myobject = MyCustomClass(* args,** kw)
$ m $ b for mylist中的元素:

if element是myobject:

test = True

break


我甚至可以写一个isinlist(elt,mylist)函数。


但是大部分时间,当我需要python中的一些基本功能时,我发现它后来实际上已经实现了
。 ;-)


所以,在python中已经有类似的东西吗?

我试着写:

''元素是在mylist''

但这似乎是不正确的语法...



没有在...中 Python中的运算符,但是你可以简单地编写你的测试




any(myobject是mylist中元素的元素)


PS:顺便说一下,如何实现设置元素比较?我的第一个

印象是'a'和'b''成员被认为是相等的如果和

只有当hash(a)== hash(b) ,但我显然错了:


>>>类A(对象):



... def __eq __(self,y):

...返回False

... def __hash__ (个体经营):

...返回5

...


>>> a = A(); b = A()
a == b



False


>>> hash(b)== hash(a)



True


>>> b in set([a])



False


>>> S = set([a])
S.difference([b])



set([< __ main __。对象位于0xb7a91dac>])


所以也有一些相等的检查,也许只有''__eq__''实现了



通常,相等性由__eq __()或__cmp __()决定。默认情况下

对象相等性检查身份。


某些容器(如内置集和字典)假设a == b暗示

hash(a)== hash(b)。


Peter


11月18日,11: 30,Peter Otten< __ pete ... @ web.dewrote:


nicolas.pource ... @ gmail.com写道:




我想测试一个对象是否在列表中(身份而不是相等

test)。

我可以写一下这样的东西:


test = False

myobject = MyCustomClass(* args,** kw)
mylist中元素的


* *如果元素是myobject:

* * * * test = True

* * * * break


我甚至可以写一个isinlist (ELT ,mylist)功能。


但是大部分时间,当我需要python中的一些基本功能时,我发现它后来发现它实际上是b $ b已经实施。 ;-)


那么,在python中已经存在类似的东西吗?

我试着写:

''元素在mylist''

但这似乎是不正确的语法...



没有在...中Python中的运算符,但是你可以简单地编写你的测试




any(myobject是mylist中元素的元素)



非常感谢

但是,任何()只有在python版本为> = 2.5时才可用,但是我可以定义一个初始化时的任何()函数,如果是python版本< 2.5


我觉得像


> ;> id(myobject)in(id(元素)in mylist中的元素)



也可以工作,也就是''不太可读,也许不那么快

(?)...


在...中运算符会很好...


PS:顺便说一下,如何实现设置元素比较?我的第一个

印象是'a'和'b''成员被认为是相等的如果和

只有当hash(a)== hash(b) ,但我显然错了:


>> A类(对象):



。 .. def __eq __(self,y):

...返回False

... def __hash __(自我):

.. 。返回5

...


>> a = A(); b = A()
a == b



False


>> hash(b)== hash(a)



True


>> b in set([a])



False


>> S = set([a])
S.difference([b ])



set([< __ main __。对象位于0xb7a91dac>])


所以也有一些平等检查,也许只有''__eq__''实现了?



通常,相等性由__eq __()或__cmp __()决定。默认情况下

对象相等性检查身份。


某些容器(如内置集和字典)假设a == b暗示

hash(a)== hash(b)。


Peter



所以,确切地说,你意思是如果hash(a)!= hash(b),a和b是

被认为是不同的,否则[即。如果hash(a)== hash(b)],a和b是

相同的当且仅当a == b?


事实上,''任何(myobject是元素在mylist中的元素)''比使用for循环慢2倍

,并且'id(myobject)in(id(element)for
$ m $ b元素在mylist中)''慢2.4倍。


Hi,

I want to test if an object IS in a list (identity and not equality
test).
I can if course write something like this :

test = False
myobject = MyCustomClass(*args, **kw)
for element in mylist:
if element is myobject:
test = True
break

and I can even write a isinlist(elt, mylist) function.

But most of the time, when I need some basic feature in python, I
discover later it is in fact already implemented. ;-)

So, is there already something like that in python ?
I tried to write :
''element is in mylist''
but this appeared to be incorrect syntax...

All objects involved all have an ''__eq__'' method.
Thanks,

N. P.

PS: Btw, how is set element comparison implemented ? My first
impression was that ''a'' and ''b'' members are considered equal if and
only if hash(a) == hash(b), but I was obviously wrong :

>>class A(object):

.... def __eq__(self,y):
.... return False
.... def __hash__(self):
.... return 5
....

>>a=A();b=A()
a==b

False

>>hash(b)==hash(a)

True

>>b in set([a])

False

>>S=set([a])
S.difference([b])

set([<__main__.A object at 0xb7a91dac>])

So there is some equality check also, maybe only if ''__eq__'' is
implemented ?

解决方案

ni***************@gmail.com wrote:

Hi,

I want to test if an object IS in a list (identity and not equality
test).
I can if course write something like this :

test = False
myobject = MyCustomClass(*args, **kw)
for element in mylist:
if element is myobject:
test = True
break

and I can even write a isinlist(elt, mylist) function.

But most of the time, when I need some basic feature in python, I
discover later it is in fact already implemented. ;-)

So, is there already something like that in python ?
I tried to write :
''element is in mylist''
but this appeared to be incorrect syntax...

There is no "is in" operator in Python, but you can write your test more
concisely as

any(myobject is element for element in mylist)

PS: Btw, how is set element comparison implemented ? My first
impression was that ''a'' and ''b'' members are considered equal if and
only if hash(a) == hash(b), but I was obviously wrong :

>>>class A(object):

... def __eq__(self,y):
... return False
... def __hash__(self):
... return 5
...

>>>a=A();b=A()
a==b

False

>>>hash(b)==hash(a)

True

>>>b in set([a])

False

>>>S=set([a])
S.difference([b])

set([<__main__.A object at 0xb7a91dac>])

So there is some equality check also, maybe only if ''__eq__'' is
implemented ?

In general equality is determined by __eq__() or __cmp__(). By default
object equality checks for identity.

Some containers (like the built-in set and dict) assume that a==b implies
hash(a) == hash(b).

Peter


On 18 juil, 11:30, Peter Otten <__pete...@web.dewrote:

nicolas.pource...@gmail.com wrote:

Hi,

I want to test if an object IS in a list (identity and not equality
test).
I can if course write something like this :

test = False
myobject = MyCustomClass(*args, **kw)
for element in mylist:
* * if element is myobject:
* * * * test = True
* * * * break

and I can even write a isinlist(elt, mylist) function.

But most of the time, when I need some basic feature in python, I
discover later it is in fact already implemented. ;-)

So, is there already something like that in python ?
I tried to write :
''element is in mylist''
but this appeared to be incorrect syntax...


There is no "is in" operator in Python, but you can write your test more
concisely as

any(myobject is element for element in mylist)

Thanks a lot
However, any() is only available if python version is >= 2.5, but I
may define a any() function on initialisation, if python version < 2.5

I think something like

>>id(myobject) in (id(element) for element in mylist)

would also work, also it''s not so readable, and maybe not so fast
(?)...

An "is in" operator would be nice...

PS: Btw, how is set element comparison implemented ? My first
impression was that ''a'' and ''b'' members are considered equal if and
only if hash(a) == hash(b), but I was obviously wrong :

>>class A(object):

... def __eq__(self,y):
... return False
... def __hash__(self):
... return 5
...

>>a=A();b=A()
a==b

False

>>hash(b)==hash(a)

True

>>b in set([a])

False

>>S=set([a])
S.difference([b])

set([<__main__.A object at 0xb7a91dac>])

So there is some equality check also, maybe only if ''__eq__'' is
implemented ?


In general equality is determined by __eq__() or __cmp__(). By default
object equality checks for identity.

Some containers (like the built-in set and dict) assume that a==b implies
hash(a) == hash(b).

Peter

So, precisely, you mean that if hash(a) != hash(b), a and b are
considered distinct, and else [ie. if hash(a) == hash(b)], a and b are
the same if and only if a == b ?


In fact, ''any(myobject is element for element in mylist)'' is 2 times
slower than using a for loop, and ''id(myobject) in (id(element) for
element in mylist)'' is 2.4 times slower.


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

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