如何检查以下所有项目是否都在列表中?
我发现有一个相关的问题,关于如何查找列表中是否至少存在一项:
如何检查是否存在以下情况之一项目在列表中?
但是,查找列表中是否所有项目都存在的最佳且Python式的方法是什么?
搜索文档我找到了这个解决方案:
>>> l = ['a', 'b', 'c']
>>> set(['a', 'b']) <= set(l)
True
>>> set(['a', 'x']) <= set(l)
False
其他解决方案是这样的:
>>> l = ['a', 'b', 'c']
>>> all(x in l for x in ['a', 'b'])
True
>>> all(x in l for x in ['a', 'x'])
False
但是在这里你必须做更多的打字。
还有其他解决方案吗?
I found, that there is related question, about how to find if at least one item exists in a list:
How to check if one of the following items is in a list?
But what is the best and pythonic way to find whether all items exists in a list?
Searching through the docs I found this solution:
>>> l = ['a', 'b', 'c']
>>> set(['a', 'b']) <= set(l)
True
>>> set(['a', 'x']) <= set(l)
False
Other solution would be this:
>>> l = ['a', 'b', 'c']
>>> all(x in l for x in ['a', 'b'])
True
>>> all(x in l for x in ['a', 'x'])
False
But here you must do more typing.
Is there any other solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Python 中的
<=
等运算符通常不会被重写为与“小于或等于”显着不同的含义。标准库这样做很不寻常——对我来说,它就像遗留的 API。使用等效且命名更清晰的方法
set.issubset
。请注意,您不需要将参数转换为集合;如果需要的话它会为你做到这一点。Operators like
<=
in Python are generally not overriden to mean something significantly different than "less than or equal to". It's unusual for the standard library does this--it smells like legacy API to me.Use the equivalent and more clearly-named method,
set.issubset
. Note that you don't need to convert the argument to a set; it'll do that for you if needed.我可能会在下面使用
set
方式:或者反过来:
我发现它更具可读性,但它可能有点过头了。集合对于计算集合之间的并集/交集/差异特别有用,但在这种情况下它可能不是最佳选择......
I would probably use
set
in the following manner :or the other way round :
I find it a bit more readable, but it may be over-kill. Sets are particularly useful to compute union/intersection/differences between collections, but it may not be the best option in this situation ...
我喜欢这两个,因为它们看起来最合乎逻辑,后者更短并且可能最快(此处使用
set
文字语法显示,该语法已 向后移植 到 Python 2.7):I like these two because they seem the most logical, the latter being shorter and probably fastest (shown here using
set
literal syntax which has been backported to Python 2.7):如果您的列表包含像这样的重复项怎么办:
集合不包含重复项。因此,以下行返回 True。
要计算重复项,您可以使用以下代码:
因此,以下行返回 False。
What if your lists contain duplicates like this:
Sets do not contain duplicates. So, the following line returns True.
To count for duplicates, you can use the code:
So, the following line returns False.
不是OP的情况,但是 - 对于任何想要在 dicts 中断言交集并由于谷歌搜索不佳而最终到达这里的人(例如我) - 你需要使用
dict.items
:这是因为 dict.items 返回键/值对的元组,并且很像 Python 中的任何对象,它们可以互换比较
Not OP's case, but - for anyone who wants to assert intersection in dicts and ended up here due to poor googling (e.g. me) - you need to work with
dict.items
:That's because
dict.items
returns tuples of key/value pairs, and much like any object in Python, they're interchangeably comparable另一种解决方案是:
我的解决方案之所以出色,是因为您可以通过将列表内联来编写单行代码。
Another solution would be:
What makes my solution great is that you can write one-liners by putting the lists inline.
如何使用 lambda 表达式执行此操作的示例如下:
An example of how to do this using a lambda expression would be:
简短的语法
我在尝试 Python 解释器时发现了一种非常可读的语法。
要搜索的项目列表
如果您有一长串要搜索的对象,保存在
sub_list
变量中:如果超集中包含任何(至少一个)项目(
或< /code> 语句):
如果所有项都包含在超集(
and
语句)中,则sub_list
是完整子集。还具有一些德摩根定律:Short syntax
I discovered a very readable syntax while experimenting on the Python interpreter.
List of items to search for
If you have a long list of objects to search for, held in a
sub_list
variable:If any (at least one) item is contained in the superset (
or
statement):If all items are contained in superset (
and
statement), thensub_list
is a full subset. Also featuring a bit of De Morgan's Law: