简体   繁体   English

Python - 在字典中查找值的交集

[英]Python - find intersection of values in a dict

Im writing a function to handle multiple queries in a boolean AND search. 我正在编写一个函数来处理布尔AND搜索中的多个查询。 I have a dict of docs where each query occurs= query_dict 我有一个docs的词典,每个查询发生在= query_dict

I want the intersection of all values in the query_dict.values(): 我想在query_dict.values()中的所有值的交集:

query_dict = {'foo': ['doc_one.txt', 'doc_two.txt', 'doc_three.txt'],
              'bar': ['doc_one.txt', 'doc_two.txt'],
              'foobar': ['doc_two.txt']}

intersect(query_dict)

>> doc_two.txt

I've been reading about intersection but I'm finding it hard to apply it to a dict. 我一直在读关于交叉的但是我发现很难将它应用于字典。

Thanks for your help! 谢谢你的帮助!

In [36]: query_dict = {'foo': ['doc_one.txt', 'doc_two.txt', 'doc_three.txt'],
              'bar': ['doc_one.txt', 'doc_two.txt'],
              'foobar': ['doc_two.txt']}

In [37]: reduce(set.intersection, (set(val) for val in query_dict.values()))
Out[37]: set(['doc_two.txt'])

In [41]: query_dict = {'foo': ['doc_one.txt', 'doc_two.txt', 'doc_three.txt'], 'bar': ['doc_one.txt', 'doc_two.txt'], 'foobar': ['doc_two.txt']} 在[41]中:query_dict = {'foo':['doc_one.txt','doc_two.txt','doc_three.txt'],'bar':['doc_one.txt','doc_two.txt'], 'foobar':['doc_two.txt']}

set.intersection(*(set(val) for val in query_dict.values())) is also a valid solution, though it's a bit slower: set.intersection(*(set(val) for val in query_dict.values()))也是一个有效的解决方案,虽然它有点慢:

In [42]: %timeit reduce(set.intersection, (set(val) for val in query_dict.values()))
100000 loops, best of 3: 2.78 us per loop

In [43]: %timeit set.intersection(*(set(val) for val in query_dict.values()))
100000 loops, best of 3: 3.28 us per loop

Another way 其他方式

first = query_dict.values()[0]
rest = query_dict.values()[1:]
print [t for t in set(first) if all(t in q for q in rest)]

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

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