在 Python 中检查集合中的项目成员资格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18300554/
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
Check item membership in set in Python
提问by grasshopper
Hello I've been coding for a couple of months now and know the basics, but I'm having a set membership problem for which I can't find a solution.
您好,我已经编写了几个月的代码并且知道基础知识,但是我遇到了一个固定的成员资格问题,我找不到解决方案。
I have a list of lists of pairs of integers, and I want to remove the list that have the "a" integer in them. I thought using sets was the easiest way. Bellow is the code:
我有一个整数对列表的列表,我想删除其中包含“a”整数的列表。我认为使用集合是最简单的方法。波纹管是代码:
## This is the item to test against.
a = set([3])
## This is the list to test.
groups = [[3, 2], [3, 4], [1, 2], [5, 4], [4, 3]]
## This is a list that will contain the lists present
## in groups which do not contain "a"
groups_no_a = []
for group in groups:
group = set(group)
if a in group:
groups_no_a.append(group)
## I thought the problem had something to do with
## clearing the variable so I put this in,
## but to no remedy.
group.clear()
print groups_no_a
I had also tried using s.issubset(t)
until I realized that this tested if everyelement in s
in t
.
我一直在使用也尝试s.issubset(t)
直到我意识到这一点,如果测试的每一个在元素s
中t
。
Thank you!
谢谢!
回答by Martijn Pieters
You want to test if there is no intersection:
您想测试是否没有交集:
if not a & group:
or
或者
if not a.intersection(group):
or, inversely, that the sets are disjoint:
或者,相反,集合是不相交的:
if a.isdisjoint(group):
The method forms take anyiterable, you don't even have to turn group
into a set for that. The following one-liner would work too:
方法形式采用任何可迭代的,您甚至不必为此group
变成一个集合。以下单行也可以:
groups_no_a = [group for group in groups if a.isdisjoint(group)]
Demo:
演示:
>>> a = set([3])
>>> groups = [[3, 2], [3, 4], [1, 2], [5, 4], [4, 3]]
>>> [group for group in groups if a.isdisjoint(group)]
[[1, 2], [5, 4]]
If all you are testing for is oneelement, then it could be that creating sets is going to cost more in performance than what you gain in testing for membership, and just doing:
如果您测试的只是一个元素,那么创建集合的性能成本可能高于您在测试成员资格时获得的性能成本,只需执行以下操作:
3 not in group
where group
is a short list.
group
短名单在哪里。
You can use the timeit
moduleto compare pieces of Python code to see what works best for your specific typical list sizes.
您可以使用该timeit
模块来比较 Python 代码片段,以了解哪些代码最适合您的特定典型列表大小。
回答by rlms
You don't need to use sets here, you can test for membership of elements in lists. You also seem to have in
, where I think you should have not in
.
此处不需要使用集合,您可以测试列表中元素的成员资格。你好像也有in
,我觉得你应该有not in
。
This code is similar to yours, and should work:
此代码与您的类似,应该可以正常工作:
## This is the item to test against.
a = 3
## This is the list to test.
groups = [[3, 2], [3, 4], [1, 2], [5, 4], [4, 3]]
## This is a list that will contain the lists present
## in groups which do not contain a
groups_no_a = []
for group in groups:
if a not in group:
groups_no_a.append(group)
print groups_no_a
However, a shorter, more Pythonic way uses list comprehensions:
然而,更短、更 Pythonic 的方式使用列表推导式:
groups_no_a = [i for i in groups if a not in i]
If you are testing a whether an item is in a much longer list, you should use sets instead for performance.
如果你正在测试一个项目是否在一个更长的列表中,你应该使用集合来代替性能。
回答by qaphla
Rather than making a = set([3]), why not do the following?
与其做一个 = set([3]),为什么不做下面的事情呢?
a = 3
groups = [[3, 2], [3, 4], [1, 2], [5, 4], [4, 3]]
groups_no_a = [group for group in groups if a not in group]
回答by lpiepiora
Maybe you could use List Comprehension:
也许你可以使用列表理解:
a = 3
groups = [[3, 2], [3, 4], [1, 2], [5, 4], [4, 3]]
print [x for x in groups if a not in x]
Edit based on a comment:
根据评论编辑:
Well to those curious, what I want to do is; I have a list like the following: [ [error, [ [group_item_1, group_item_2], [...], [...], [...] ] ], [more like this previous], [...] ], and I want to get the item with least error and that doesn't have "a" in group_item_1 or group_item_2. The lists are already sorted by error. I sorta almost go it :D
对于那些好奇的人来说,我想做的是;我有一个如下所示的列表:[[error, [ [group_item_1, group_item_2], [...], [...], [...] ] ], [more like this previous], [... ] ],我想获得错误最少的项目,并且在 group_item_1 或 group_item_2 中没有“a”。列表已按错误排序。我差不多可以了:D
This should do the trick:
这应该可以解决问题:
from itertools import chain, iterfilter
def flatten(listOfLists):
"Flatten one level of nesting"
return chain.from_iterable(listOfLists)
errors_list = [ ['error0', [ [30, 2], [3, 4], [1, 2], [5, 4], [4, 3] ] ], ['error1', [ [31, 2], [3, 4], [1, 2], [5, 4], [4, 3] ] ] ]
a = 30
result = next(ifilter(lambda err: a not in flatten(err[1]), reversed(errors_list)), None)
print result #finds error1 as it has no 30 on its list