2016-01-14 53 views
2

我有4個列表,列表中的每個元素在整個列表中都是唯一的。如何找到該值是否存在於列表中的某個列表中並返回它存在的列表?查找多個列表中是否存在值

列表示例:

value = 'a' 
a = ['a','b','c'] 
b = ['d','e','f'] 
d = ['g','h','i'] 
c = ['j','k','l'] 

我的預期輸出是當值所在的列表名稱: 對於我的期望輸出上面的例子是:

一個

+0

什麼值是在列表中,他們是其他列表或字符串值列表或數字... –

+9

'[我爲我在[A,B,C ,d]如果我]值' –

+0

你想要的變量保存要返回的列表,或者在發現這種情況的列表(一個或多個)的內容是什麼?即您的預期產出是什麼? –

回答

2

鑑於你的更新問題,讓我們假設a, b, c, d變量是在全球範圍內

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 

ie全局命名空間中第一個列表的名稱value

如果變量在本地範圍內,在一個函數中,你可以使用locals()代替

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_作爲前綴

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外殼

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()返回一個字典。它的鍵是在全局命名空間中定義的變量的名稱。它的值是每個這樣的變量所持有的值。

因此

>>> 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

+0

感謝你提供不同的解決方案。你怎麼沒有打電話給任何名單? @Pynchia – essramos

+0

請看到我的編輯添加更多細節:) – Pynchia

+1

謝謝你,我現在明白@Pynchia – essramos

7

你可以使用list comprehension

>>> value ="a" 
>>> a = ['a','b','c'] 
>>> b = ['d','e','f'] 
>>> d = ['g','h','i'] 
>>> c = ['j','k','l'] 
>>> [x for x in a,b,c,d if value in x] 
[['a', 'b', 'c']] 

對於g等變量名:

>>> for x in a,b,c,d: 
...  if value in x: 
...   for key,val in locals().items(): 
...    if x == val: 
...     print key 
...     break 
... 
a 

當地人()包含本地範圍變量作爲解釋,變量名是關鍵,它的值作爲值

全局包含全局範圍變量作爲解釋,變量名是關鍵它的值作爲值

3

如果你不想使用列表理解,你總是可以使用一個令人費解和詳細的lambda表達式!

list(filter(lambda x: value in x, [a,b,c,d]))

編輯:問題是更新索要變種名稱,而不是VAR值

2

你所試圖做的是幾乎總是一個壞主意。假設你得到一個包含你想要的列表名字的字符串。那麼你用它做什麼?如果你所擁有的只是一個包含它的名字的字符串,那麼沒有很好的方法來獲得一個變量。你可以用globals()locals()做到這一點,但那些不可避免地使你的代碼混亂和困惑。

正確的方式做,這是使用列表或字典,收集所有您想要搜索通過名單。遍歷單個對象比遍歷全局或局部範圍中的每個名稱要好得多。

value = "a" 
named_lists = { 
    "a": ['a','b','c'], 
    "b": ['d','e','f'], 
    "d": ['g','h','i'], 
    "c": ['j','k','l'] 
} 

names = [name for name, seq in named_lists.items() if value in seq] 
print(names) 
if names: #there may be multiple lists that contain `value`, so let's just look at the first one, if it exists 
    first_name = names[0] 
    print(named_lists[first_name]) 

結果:

['a'] 
['a', 'b', 'c']