• <bdo id='dKyat'></bdo><ul id='dKyat'></ul>
  • <small id='dKyat'></small><noframes id='dKyat'>

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

      1. 元组索引的 Python `dict`:获取一块饼

        时间:2023-08-30
          • <tfoot id='WLjLj'></tfoot>
          • <legend id='WLjLj'><style id='WLjLj'><dir id='WLjLj'><q id='WLjLj'></q></dir></style></legend>
              <bdo id='WLjLj'></bdo><ul id='WLjLj'></ul>

                <tbody id='WLjLj'></tbody>

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

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

                  本文介绍了元组索引的 Python `dict`:获取一块饼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有

                  my_dict = {
                    ("airport", "London"): "Heathrow",
                    ("airport", "Tokyo"): "Narita",
                    ("hipsters", "London"): "Soho"
                  }
                  

                  什么是从字典中取出所有机场的有效(不扫描所有键)但优雅的方法,即预期输出 ["Heathrow", "Narita"].在可以按元组索引的数据库中,通常可以执行类似

                  What is an efficient (no scanning of all keys), yet elegant way to get all airports out of this dictionary, i.e. expected output ["Heathrow", "Narita"]. In databases that can index by tuples, it's usually possible to do something like

                  airports = my_dict.get(("airport",*))
                  

                  (但通常只有星"位于元组中最右边的位置,因为索引通常只以一种顺序存储).

                  (but usually only with the 'stars' sitting at the rightmost places in the tuple since the index usually is only stored in one order).

                  由于我想象 Python 以类似的方式使用元组键索引字典(使用键的固有顺序),我想可能有一种方法可以用来以这种方式对索引进行切片?

                  Since I imagine Python to index dictionary with tuple keys in a similar way (using the keys's inherent order), I imagine there might be a method I could use to slice the index this way?

                  Edit1:添加预期输出

                  Added expected output

                  Edit2:删除了最后一个短语.在条件中添加了(不扫描所有键)"以使其更清晰.

                  Removed last phrase. Added '(no scanning of all keys)' to the conditions to make it clearer.

                  推荐答案

                  您的数据当前的组织方式不允许有效查找 - 基本上您必须扫描所有键.

                  The way your data is currently organized doesn't allow efficient lookup - essentially you have to scan all the keys.

                  字典是幕后的哈希表,访问值的唯一方法是获取键的哈希 - 为此,您需要 整个键.

                  Dictionaries are hash tables behind the scenes, and the only way to access a value is to get the hash of the key - and for that, you need the whole key.

                  使用这样的嵌套层次结构,因此您可以进行直接 O(1) 查找:

                  Use a nested hierarchy like this, so you can do a direct O(1) lookup:

                  my_dict = {
                    "airport": {
                       "London": "Heathrow",
                       "Tokyo": "Narita",
                    },
                    "hipsters": {
                       "London": "Soho"
                    }
                  }
                  

                  这篇关于元组索引的 Python `dict`:获取一块饼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python:这两个 import 语句有什么区别? 下一篇:Python 元组...不是元组吗?逗号有什么作用?

                  相关文章

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

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

                      <bdo id='cBG8Q'></bdo><ul id='cBG8Q'></ul>