相交两个字典 [英] Intersecting two dictionaries

查看:80
本文介绍了相交两个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个反向索引的搜索程序.索引本身是一个字典,其关键字是术语,其值本身是短文档的字典,ID号为关键字,其文本内容为值.

I am working on a search program over an inverted index. The index itself is a dictionary whose keys are terms and whose values are themselves dictionaries of short documents, with ID numbers as keys and their text content as values.

要对两个词进行与"搜索,因此我需要与他们的发文列表(字典)相交.什么是在Python中执行此操作的清晰(不一定过于聪明)的方法?我首先通过 iter 进行了长期尝试:

To perform an 'AND' search for two terms, I thus need to intersect their postings lists (dictionaries). What is a clear (not necessarily overly clever) way to do this in Python? I started out by trying it the long way with iter:

p1 = index[term1]  
p2 = index[term2]
i1 = iter(p1)
i2 = iter(p2)
while ...  # not sure of the 'iter != end 'syntax in this case
...

推荐答案

通常,要在Python中构造字典的交集,您可以首先使用

In general, to construct the intersection of dictionaries in Python, you can first use the & operator to calculate the intersection of sets of the dictionary keys (dictionary keys are set-like objects in Python 3):

dict_a = {"a": 1, "b": 2}
dict_b = {"a": 2, "c": 3} 

intersection = dict_a.keys() & dict_b.keys()  # {'a'}

在Python 2上,您必须将字典键转换为自己设置:

On Python 2 you have to convert the dictionary keys to sets yourself:

keys_a = set(dict_a.keys())
keys_b = set(dict_b.keys())
intersection = keys_a & keys_b

然后给定键的交集,然后可以构建值的交集,但是这是需要的.您必须在这里做出选择,因为集合相交的概念不会告诉您如果关联的值不同,该怎么办.(这可能就是为什么未在Python中直接为字典定义& 交集运算符的原因.

Then given the intersection of the keys, you can then build the intersection of your values however is desired. You have to make a choice here, since the concept of set intersection doesn't tell you what to do if the associated values differ. (This is presumably why the & intersection operator is not defined directly for dictionaries in Python).

在这种情况下,听起来您同一个键的值将相等,因此您只需从其中一个词典中选择一个值即可.

In this case it sounds like your values for the same key would be equal, so you can just choose the value from one of the dictionaries:

dict_of_dicts_a = {"a": {"x":1}, "b": {"y":3}}
dict_of_dicts_b = {"a": {"x":1}, "c": {"z":4}} 

shared_keys = dict_of_dicts_a.keys() & dict_of_dicts_b.keys()

# values equal so choose values from a:
dict_intersection = {k: dict_of_dicts_a[k] for k in shared_keys }  # {"a":{"x":1}}

组合值的其他合理方法将取决于字典中值的类型及其代表的含义.例如,您可能还需要字典的共享键的值的并集.由于字典的联合不依赖于值,因此定义明确,在python中,您可以使用 | 运算符获取它:

Other reasonable methods of combining values would depend on the types of the values in your dictionaries, and what they represent. For example you might also want the union of values for shared keys of dictionaries of dictionaries. Since the union of dictionaries doesn't depend on the values, it is well defined, and in python you can get it using the | operator:

# union of values for each key in the intersection:
dict_intersection_2 = { k: dict_of_dicts_a[k] | dict_of_dicts_b[k] for k in shared_keys }

在这种情况下,两个键中的键"a" 具有相同的字典值,则结果相同.

Which in this case, with identical dictionary values for key "a" in both, would be the same result.

这篇关于相交两个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆