• <legend id='CLypc'><style id='CLypc'><dir id='CLypc'><q id='CLypc'></q></dir></style></legend>
    • <bdo id='CLypc'></bdo><ul id='CLypc'></ul>

    1. <i id='CLypc'><tr id='CLypc'><dt id='CLypc'><q id='CLypc'><span id='CLypc'><b id='CLypc'><form id='CLypc'><ins id='CLypc'></ins><ul id='CLypc'></ul><sub id='CLypc'></sub></form><legend id='CLypc'></legend><bdo id='CLypc'><pre id='CLypc'><center id='CLypc'></center></pre></bdo></b><th id='CLypc'></th></span></q></dt></tr></i><div id='CLypc'><tfoot id='CLypc'></tfoot><dl id='CLypc'><fieldset id='CLypc'></fieldset></dl></div>

        <small id='CLypc'></small><noframes id='CLypc'>

      1. <tfoot id='CLypc'></tfoot>
      2. 交叉两个字典

        Intersecting two dictionaries(交叉两个字典)
          <bdo id='HiZB3'></bdo><ul id='HiZB3'></ul>

          <small id='HiZB3'></small><noframes id='HiZB3'>

          <i id='HiZB3'><tr id='HiZB3'><dt id='HiZB3'><q id='HiZB3'><span id='HiZB3'><b id='HiZB3'><form id='HiZB3'><ins id='HiZB3'></ins><ul id='HiZB3'></ul><sub id='HiZB3'></sub></form><legend id='HiZB3'></legend><bdo id='HiZB3'><pre id='HiZB3'><center id='HiZB3'></center></pre></bdo></b><th id='HiZB3'></th></span></q></dt></tr></i><div id='HiZB3'><tfoot id='HiZB3'></tfoot><dl id='HiZB3'><fieldset id='HiZB3'></fieldset></dl></div>

          <tfoot id='HiZB3'></tfoot>
              <tbody id='HiZB3'></tbody>
                  <legend id='HiZB3'><style id='HiZB3'><dir id='HiZB3'><q id='HiZB3'></q></dir></style></legend>
                  本文介绍了交叉两个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个基于倒排索引的搜索程序.索引本身是一个字典,其键是术语,其值本身就是短文档的字典,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.

                  要对两个术语执行AND"搜索,因此我需要交叉它们的发布列表(字典).在 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中构造字典的交集,可以先使用& operator 计算字典键集合的交集(字典键是 Python 3 中的类似集合的对象):

                  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.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                  • <bdo id='hwFfH'></bdo><ul id='hwFfH'></ul>
                  • <i id='hwFfH'><tr id='hwFfH'><dt id='hwFfH'><q id='hwFfH'><span id='hwFfH'><b id='hwFfH'><form id='hwFfH'><ins id='hwFfH'></ins><ul id='hwFfH'></ul><sub id='hwFfH'></sub></form><legend id='hwFfH'></legend><bdo id='hwFfH'><pre id='hwFfH'><center id='hwFfH'></center></pre></bdo></b><th id='hwFfH'></th></span></q></dt></tr></i><div id='hwFfH'><tfoot id='hwFfH'></tfoot><dl id='hwFfH'><fieldset id='hwFfH'></fieldset></dl></div>
                      <tbody id='hwFfH'></tbody>

                    <small id='hwFfH'></small><noframes id='hwFfH'>

                        <tfoot id='hwFfH'></tfoot>

                            <legend id='hwFfH'><style id='hwFfH'><dir id='hwFfH'><q id='hwFfH'></q></dir></style></legend>