在 python 字典中查找对应字典值中元素交集的键
Finding the keys in a python dictionary for intersection of elements in the corresponding dictionary values
我有以下 Python 词典:
{(a,b,c): [(1, 2, 3), (4, 5, 6)], (d,e,f):[(7,8,9)] ,(m, n, o): [(1, 2, 3), (7, 8, 9)]}
我正在尝试查找与列表中元组元素的交集对应的键,格式如下:
对于元组 (1,2,3),交集的输出应为:
(a,b,c), (m,n,o)
虽然可以迭代列表中的每个元组元素作为字典值并找到相应的键,但我正在寻找一种 pythonic 方法来实现这一点。
>>> dic = {('a','b','c'): [(1, 2, 3), (4, 5, 6)], ('d','e','f'):[(7,8,9)] ,('m', 'n', 'o'): [(1, 2, 3), (7, 8, 9)]}
>>> [i for i in dic if (1,2,3) in dic[i]]
[('a', 'b', 'c'), ('m', 'n', 'o')]
>>>
如果不想遍历,可以使用filter
>>> list(filter(lambda x:(1,2,3) in dic[x],dic))
[('a', 'b', 'c'), ('m', 'n', 'o')]
如果您是模块爱好者,请使用itertools.takewhile
。
>>> from itertools import takewhile
>>> list(takewhile(lambda x:(1,2,3) in dic[x],dic))
[('a', 'b', 'c'), ('m', 'n', 'o')]
最后,上面的 none 将做正确的事情,而无需在内部/外部迭代集合。这个花哨的模块是一种包装器,在引擎盖下做或多或少相同的事情。
你原来的字典有语法错误,除非变量a、b、c等之前已经定义过。但这里有一个答案:
[key for key in d if (1,2,3) in d[key]]
可能 filter() 是选项之一:
d = {('a','b','c'): [(1, 2, 3), (4, 5, 6)], ('d','e','f'):[(7,8,9)] ,('m', 'n', 'o'): [(1, 2, 3), (7, 8, 9)]}
def find_keys(my_dict, value):
l = lambda x: value in my_dict[x]
return list(filter(l, my_dict))
result = find_keys(d, (1, 2, 3))
如果您反转列表字典:
d = {('a','b','c'): [(1, 2, 3), (4, 5, 6)], ('d','e','f'):[(7,8,9)] ,('m', 'n', 'o'): [(1, 2, 3), (7, 8, 9)]}
i = {}
for k,v in d.items():
for e in v:
i.setdefault(e, []).append(k)
i
的内容:
>>> i
{(4, 5, 6): [('a', 'b', 'c')], (7, 8, 9): [('d', 'e', 'f'), ('m', 'n', 'o')], (1, 2, 3): [('a', 'b', 'c'), ('m', 'n', 'o')]}
您可以这样查询 (1,2,3)
:
>>> i[(1,2,3)]
[('a', 'b', 'c'), ('m', 'n', 'o')]
我有以下 Python 词典:
{(a,b,c): [(1, 2, 3), (4, 5, 6)], (d,e,f):[(7,8,9)] ,(m, n, o): [(1, 2, 3), (7, 8, 9)]}
我正在尝试查找与列表中元组元素的交集对应的键,格式如下:
对于元组 (1,2,3),交集的输出应为:
(a,b,c), (m,n,o)
虽然可以迭代列表中的每个元组元素作为字典值并找到相应的键,但我正在寻找一种 pythonic 方法来实现这一点。
>>> dic = {('a','b','c'): [(1, 2, 3), (4, 5, 6)], ('d','e','f'):[(7,8,9)] ,('m', 'n', 'o'): [(1, 2, 3), (7, 8, 9)]}
>>> [i for i in dic if (1,2,3) in dic[i]]
[('a', 'b', 'c'), ('m', 'n', 'o')]
>>>
如果不想遍历,可以使用filter
>>> list(filter(lambda x:(1,2,3) in dic[x],dic))
[('a', 'b', 'c'), ('m', 'n', 'o')]
如果您是模块爱好者,请使用itertools.takewhile
。
>>> from itertools import takewhile
>>> list(takewhile(lambda x:(1,2,3) in dic[x],dic))
[('a', 'b', 'c'), ('m', 'n', 'o')]
最后,上面的 none 将做正确的事情,而无需在内部/外部迭代集合。这个花哨的模块是一种包装器,在引擎盖下做或多或少相同的事情。
你原来的字典有语法错误,除非变量a、b、c等之前已经定义过。但这里有一个答案:
[key for key in d if (1,2,3) in d[key]]
可能 filter() 是选项之一:
d = {('a','b','c'): [(1, 2, 3), (4, 5, 6)], ('d','e','f'):[(7,8,9)] ,('m', 'n', 'o'): [(1, 2, 3), (7, 8, 9)]}
def find_keys(my_dict, value):
l = lambda x: value in my_dict[x]
return list(filter(l, my_dict))
result = find_keys(d, (1, 2, 3))
如果您反转列表字典:
d = {('a','b','c'): [(1, 2, 3), (4, 5, 6)], ('d','e','f'):[(7,8,9)] ,('m', 'n', 'o'): [(1, 2, 3), (7, 8, 9)]}
i = {}
for k,v in d.items():
for e in v:
i.setdefault(e, []).append(k)
i
的内容:
>>> i
{(4, 5, 6): [('a', 'b', 'c')], (7, 8, 9): [('d', 'e', 'f'), ('m', 'n', 'o')], (1, 2, 3): [('a', 'b', 'c'), ('m', 'n', 'o')]}
您可以这样查询 (1,2,3)
:
>>> i[(1,2,3)]
[('a', 'b', 'c'), ('m', 'n', 'o')]