English 中文(简体)
图一清单的两个“集装箱”业务之间的差异
原标题:Difference between two "contains" operations for python lists

I m fairly new to python and have found that I need to query a list about whether it contains a certain item.
The majority of the postings I have seen on various websites (including this similar stackoverflow question) have all suggested something along the lines of

for i in list
    if i == thingIAmLookingFor
        return True

然而,我也从一个论坛发现:

if thingIAmLookingFor in list
    # do work

工作。

我很想知道,<代码>如果在清单方法中为<代码>()中的一,或者该编码是否得到不同执行。

我也希望,如果是这样的话,我更愿意这样做。

最佳回答

在你简单的例子中,最好使用<条码>。

您与、indoesn t work (至少是直接的)因为禁止化学武器组织并不希望找到一个与东西相等的物体,而一个标的attribute n等于某种物体。

One answer does mention using in on a list comprehension, though I m not sure why a generator expression wasn t used instead:

if 5 in (data.n for data in myList):
    print "Found it"

但与其他方法相比,例如使用<条码>的任何<<>条/代码>的“条码”的方法相比,这种改进几乎没有多大。

if any(data.n == 5 for data in myList):
    print "Found it"
问题回答

强烈倾向于采用“X:......”格式,不仅因为格式使用较少,而且涉及其他数据类型,而且(对我来说)更容易阅读。

我不敢肯定它是如何实施的,但我期望它能大大提高储存在更可检索表格中的数据类型的效率。 比如,确定或定型钥匙。

某些名单中的<代码>是首选和最快的方式。

使用内部操作器的主次将“编码”改为“编码>,__contains__(thing),其实施相当于:any(x is me or x =s) for x in somelist)

注:状况测试特性,然后是平等。

for i in list
    if i == thingIAmLookingFor
        return True

以上是检验某项物品在收集中是否存在的可怕方法。 <returns<>/em>True,从该功能中删除,这样,如果你需要作为某些代码的一部分进行测试,那么你就必须将这一功能移至单独的实用功能中,或添加thingWasFound = False,并在声明(和随后的突破)中将其编为True,其中任一条系可作简单表述的碎块。

加上,如果你刚刚使用thingIAmLooking 在清单中,might通过开展较少的“灰色”作业(需要做同样的作业,但可能在C进行,因为<>list>/code>是一种内在的。 但更重要的是,如果<代码>>list实际上受到诸如一套或一套字典<代码>的其他一些收集活动的约束,则《国际仲裁裁决汇编》 在名单中,将使用散射装置,提供这种类型的支持,并在使用<<>>><>>>>条码时提高效率。 lo将迫使沙尔转过每一个物品。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...