Python,发现一个列表没有特定的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4450752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:48:20  来源:igfitidea点击:

Python, find out that a list does not have specific item

pythonif-statement

提问by Maks

Let's say there is a list a that contains both numbers and letters. Is there quick way to find out that the list doesn't contain some specific element. I plan to use it in conditions.

假设有一个包含数字和字母的列表 a。有没有快速的方法来找出列表不包含某些特定元素。我打算在条件下使用它。

回答by Sven Marnach

Maybe

也许

3 not in [1, 2, "a"]
# True

回答by Spacedman

Note: If you can put the elements as keys of a dict then testing for membership is much quicker thanks to the hashing algorithm. Will only be an issue if your list is very long or your app does a lot of this kind of thing. Otherwise, "X not in Y" as Sven says.

注意:如果您可以将元素作为 dict 的键,那么由于散列算法,成员资格测试会快得多。只有当你的列表很长或者你的应用程序做了很多这样的事情时才会出现问题。否则,正如斯文所说,“X 不在 Y 中”。

回答by Ant

do you mean

你的意思是

bool([x for x in alist if isinstance(x, int)])

better version (by Sven Marnach):

更好的版本(由 Sven Marnach 提供):

any(isinstance(x, int) for x in alist)

?

?

回答by David Cournapeau

It depends on what you are trying to do. If speed does not matter, then use a in lst. If it does matter, it will depend on whether you can afford converting your list to a different data structure first (i.e. will you often look for items in the say list), size, etc...

这取决于您要尝试做什么。如果速度无关紧要,则使用 in lst。如果确实重要,这将取决于您是否有能力首先将列表转换为不同的数据结构(即您是否经常在列表中查找项目)、大小等...

To give an idea:

给出一个想法:

import timeit

a = range(10000)
da = dict(zip(a, [None for i in a]))

def list_first():
    return 0 in a

def dict_first():
    return 0 in da

def list_last():
    return 9999 in a

def dict_last():
    return 9999 in da

if __name__ == "__main__":
    for f in ["list_first", "dict_first", "list_last", "dict_last"]:
        t = timeit.Timer("%s()" % f, setup="from __main__ import %s" % f)
        print min(t.repeat(number=10000))

This gives me:

这给了我:

0.00302004814148
0.00318598747253
4.21943712234
0.004145860672

If you look for an item which is at the beginning of the list, using a dict does not speed things up, as expected. If you look for an item at the end, then the difference is very significant (3 order of magnitude), although as expected: dict use hashtable, lists needs to look for each item one after the other.

如果您查找位于列表开头的项目,则使用 dict 不会像预期的那样加快速度。如果你在最后寻找一个项目,那么差异是非常显着的(3 个数量级),尽管正如预期的那样:dict 使用哈希表,列表需要一个接一个地查找每个项目。

If items are comparable, you can also get a significant speed up by sorting your sequence and using a binary search (log(N) instead of N, log(N) being relatively fast compared O(1) for N not too big in practice for python), or using more advanced structures (binary search tree, etc...). It can get pretty complex - data structures for fast search is one of the most studied problem in CS after all.

如果项目具有可比性,您还可以通过对序列进行排序并使用二分搜索(log(N) 而不是 N,log(N) 相对较快,相比 O(1) 而言,N 在实践中不会太大)来获得显着的加速对于python),或者使用更高级的结构(二叉搜索树等)。它可能变得非常复杂 - 毕竟用于快速搜索的数据结构是 CS 中研究最多的问题之一。