查找值是否存在于多个列表中
我有4个列表,并且列表中的每个元素在这4个列表中都是唯一的.如何查找该值是否存在于列表之一中,并返回该值存在于哪个列表中?
I have 4 list and each element on the list are unique across the 4 list. How do I find if the value exist in one of the list and return which list it exist?
示例列表:
value = 'a'
a = ['a','b','c']
b = ['d','e','f']
d = ['g','h','i']
c = ['j','k','l']
我的预期输出是找到值的列表的名称: 对于上面的示例,我的预期输出将是:
My expected output is the name of the list where the value is found: For the example above my expected output would be:
a
鉴于您的更新问题,我们假设a, b, c, d
变量在全局范围内
Given your updated question, let's assume the a, b, c, d
variables are in the global scope
value = 'a'
a = ['a','b','c']
b = ['d','e','f']
d = ['g','h','i']
c = ['j','k','l']
w = next(n for n,v in filter(lambda t: isinstance(t[1],list), globals().items()) if value in v)
print(w)
产生
a
即包含value
如果变量在本地范围内,例如在函数中,您可以改用locals()
If the variables are in a local scope, e.g. within a function, you can use locals()
instead
def f():
a = ['a','b','c']
b = ['d','e','f']
d = ['g','h','i']
c = ['j','k','l']
w = next(n for n,v in filter(lambda t: isinstance(t[1],list), locals().items()) if value in v)
print(w)
f()
产生
a
注意:您可能希望采用命名约定来定位特定的变量组,例如targ_
作为前缀
Note: you might want to adopt a naming convention to target a specific group of variables, e.g. targ_
as a prefix
targ_a = ['a','b','c']
targ_b = ['d','e','f']
targ_d = ['g','h','i']
targ_c = ['j','k','l']
w = next(n for n,v in filter(lambda t: isinstance(t[1],list) and t[0].startswith('targ_'), globals().items()) if value in v)
print(w)
产生
targ_a
为了更详细地说明事情,让我们看一下globals()
调用返回的内容.例如使用python shell
To explain things a bit more in detail, let's have a look at what the globals()
call returns. For example using the python shell
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> value = 'a'
>>> targ_a = ['a','b','c']
>>> targ_b = ['d','e','f']
>>> targ_d = ['g','h','i']
>>> targ_c = ['j','k','l']
>>> globals()
{'targ_d': ['g', 'h', 'i'], 'targ_c': ['j', 'k', 'l'],
'__builtins__': <module 'builtins' (built-in)>,
'__doc__': None, '__package__': None,
'__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'targ_a': ['a', 'b', 'c'], '__name__': '__main__',
'targ_b': ['d', 'e', 'f'], '__spec__': None, 'value': 'a'}
如您所见,
globals()
返回一个字典.它的键是在全局名称空间中定义的变量的名称.它的值就是每个这样的变量所保存的值.
as you can see, globals()
returns a dictionary. Its keys are the names of the variables defined in the global namespace. Its values are the value held by each such variable.
因此
>>> next(n for n,v in filter(lambda t: isinstance(t[1],list) and t[0].startswith('targ_'), globals().items()) if value in v)
'targ_a'
在表达式生成的生成器上重复一次,该生成器在全局命名空间中生成每个名称,该名称对应于名称以targ_
开头且包含等于value
的元素的列表.通过对调用globals
iterates once on the generator produced by the expression, which yields each name in the global namespace that corresponds to a list whose name starts with targ_
and that contains an element equal to value
. It does so by iterating on the dictionary returned by the call to globals