简体   繁体   English

查找值属于多个集合中的哪个

[英]Find in which of multiple sets a value belongs to

I have several sets of values, and need to check in which of some of them a given value is located, and return the name of that set. 我有几组值,需要检查给定值位于其中哪些值中,并返回该组的名称。

value = 'a'
set_1 = {'a', 'b', 'c'}
set_2 = {'d', 'e', 'f'}
set_3 = {'g', 'h', 'i'}
set_4 = {'a', 'e', 'i'}

I'd like to check if value exists in sets 1-3, without including set_4 in the method, and return the set name. 我想检查值是否存在于集合1-3中,而方法中不包括set_4,并返回集合名称。 So something like: 所以像这样:

find_set(value in set_1, set_2, set_3)

should return 应该回来

set_1

Maybe some neat lambda function? 也许一些整齐的lambda函数? I tried 我试过了

w = next(n for n,v in filter(lambda t: isinstance(t[1],set), globals().items()) if value in v)

from Find if value exists in multiple lists but that approach checks ALL local/global sets. Find中查找值是否存在于多个列表中,但是该方法将检查所有本地/全局集。 That won't work here, because the value can exist in several of them. 这在这里行不通,因为值可以存在于其中几个中。 I need to be able to specify in which sets to look. 我需要能够指定要查看的集合。

Don't use an ugly hackish lambda which digs in globals so you can get a name; 不要使用会在globals中挖掘的丑陋的hacklamdmbda,这样您就可以获得名称。 that will confuse anyone reading your code including yourself after a few weeks :-). 几个星期后,包括您自己在内的任何人都可能会读懂您的代码:-)。

You want to be able to get a name for sets you have defined, well, this is why we have dictionaries. 您希望能够为已定义的集合命名,这就是为什么我们有字典的原因。 Make a dictionary out of your sets and then you can create handy/readable set/list comprehensions to get what you want in a compact readable fashion: 用您的集合制作字典,然后您可以创建方便/可读的集合/列表理解,以紧凑的可读方式获得所需内容:

>>> d = {'set_1': set_1, 'set_2': set_2, 'set_3': set_3, 'set_4': set_4}

To catch all sets in which 'a' is located: 要捕获'a'所在的所有集合:

>>> {name for name, items in d.items() if 'a' in items}
{'set_1', 'set_4'}

To exclude some name add another the required clause to the if for filtering: 要排除某些名称,请在if添加另一个required子句以进行过滤:

>>> {name for name, items in d.items() if 'a' in items and name != 'set_4'}
{'set_1'}

You can of course factor this into a function and be happy you'll be able to understand it if you bump into it in the future: 当然,您可以将其分解为一个函数,并且很高兴将来遇到它时能够理解它:

def find_sets(val, *excludes, d=d):
    return {n for n, i in d.items() if val in i and n not in excludes}

This behaves in a similar way as the previous. 这与以前的行为类似。 d=d is probably not the way you want to do it, you'll probably be better of using some **d syntax for this. d=d可能不是您想要的方式,为此使用一些**d语法可能会更好。


If you just want to get the first value, return the next(comprehension) from your function like this: 如果只想获取第一个值,请从函数中返回next(comprehension) ,如下所示:

def find_sets(val, *excludes, d=d):
    return next((n for n, i in d.items() if val in i and n not in excludes), '')

The '' just indicates a default value to be returned if no elements are actually found, that is, when called with a value that isn't present, an empty string will be returned (subject to change according to your preferences): ''表示如果未实际找到任何元素,则返回默认值,即当使用不存在的值调用时,将返回一个空字符串(可根据您的喜好进行更改):

>>> find_sets('1')
''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM