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

        <small id='3xUQT'></small><noframes id='3xUQT'>

        为什么我不能在 python 中使用列表作为 dict 键?

        Why can#39;t I use a list as a dict key in python?(为什么我不能在 python 中使用列表作为 dict 键?)
      1. <i id='lxJBG'><tr id='lxJBG'><dt id='lxJBG'><q id='lxJBG'><span id='lxJBG'><b id='lxJBG'><form id='lxJBG'><ins id='lxJBG'></ins><ul id='lxJBG'></ul><sub id='lxJBG'></sub></form><legend id='lxJBG'></legend><bdo id='lxJBG'><pre id='lxJBG'><center id='lxJBG'></center></pre></bdo></b><th id='lxJBG'></th></span></q></dt></tr></i><div id='lxJBG'><tfoot id='lxJBG'></tfoot><dl id='lxJBG'><fieldset id='lxJBG'></fieldset></dl></div>

            • <bdo id='lxJBG'></bdo><ul id='lxJBG'></ul>

              1. <small id='lxJBG'></small><noframes id='lxJBG'>

                    <tbody id='lxJBG'></tbody>
                  <tfoot id='lxJBG'></tfoot>

                  <legend id='lxJBG'><style id='lxJBG'><dir id='lxJBG'><q id='lxJBG'></q></dir></style></legend>
                  本文介绍了为什么我不能在 python 中使用列表作为 dict 键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对什么可以/不能用作 python dict 的键有点困惑.

                  I'm a bit confused about what can/can't be used as a key for a python dict.

                  dicked = {}
                  dicked[None] = 'foo'     # None ok
                  dicked[(1,3)] = 'baz'    # tuple ok
                  import sys
                  dicked[sys] = 'bar'      # wow, even a module is ok !
                  dicked[(1,[3])] = 'qux'  # oops, not allowed
                  

                  所以元组是一种不可变类型,但如果我在其中隐藏一个列表,那么它就不能成为键.. 我不能像在模块内一样轻松地隐藏一个列表吗?

                  So a tuple is an immutable type but if I hide a list inside of it, then it can't be a key.. couldn't I just as easily hide a list inside a module?

                  我有一个模糊的想法,即密钥必须是可散列的",但我承认我自己对技术细节一无所知;我不知道这里到底发生了什么.如果您尝试将列表用作键,而将哈希用作它们的内存位置,会出现什么问题?

                  I had some vague idea that that the key has to be "hashable" but I'm just going to admit my own ignorance about the technical details; I don't know what's really going on here. What would go wrong if you tried to use lists as keys, with the hash as, say, their memory location?

                  推荐答案

                  在 Python wiki 中有一篇关于该主题的好文章:Why列表不能是字典键.如那里所述:

                  There's a good article on the topic in the Python wiki: Why Lists Can't Be Dictionary Keys. As explained there:

                  如果您尝试使用列表作为键,而将哈希作为它们的内存位置,会出现什么问题?

                  What would go wrong if you tried to use lists as keys, with the hash as, say, their memory location?

                  它可以在没有真正破坏任何要求的情况下完成,但它会导致意外行为.列表通常被视为其值源自其内容的值,例如在检查(不)相等性时.许多人会 - 可以理解 - 期望您可以使用任何列表 [1, 2] 来获得相同的密钥,而您必须保持完全相同的列表对象.但是,一旦用作键的列表被修改,按值查找就会中断,并且对于按标识查找要求您保持完全相同的列表 - 这对于任何其他常见的列表操作都不是必需的(至少我没有想到).

                  It can be done without really breaking any of the requirements, but it leads to unexpected behavior. Lists are generally treated as if their value was derived from their content's values, for instance when checking (in-)equality. Many would - understandably - expect that you can use any list [1, 2] to get the same key, where you'd have to keep around exactly the same list object. But lookup by value breaks as soon as a list used as key is modified, and for lookup by identity requires you to keep around exactly the same list - which isn't requires for any other common list operation (at least none I can think of).

                  其他对象,如模块和 object 无论如何都会从它们的对象标识中获得更大的收益(您上一次拥有两个名为 sys 的不同模块对象是什么时候?),并且无论如何都要进行比较.因此,当它们用作 dict 键时,在这种情况下也会按身份进行比较,这并不令人惊讶 - 甚至是预期的.

                  Other objects such as modules and object make a much bigger deal out of their object identity anyway (when was the last time you had two distinct module objects called sys?), and are compared by that anyway. Therefore, it's less surprising - or even expected - that they, when used as dict keys, compare by identity in that case as well.

                  这篇关于为什么我不能在 python 中使用列表作为 dict 键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                  • <legend id='vRuPZ'><style id='vRuPZ'><dir id='vRuPZ'><q id='vRuPZ'></q></dir></style></legend>

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

                          <tbody id='vRuPZ'></tbody>

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

                        <tfoot id='vRuPZ'></tfoot>
                          • <bdo id='vRuPZ'></bdo><ul id='vRuPZ'></ul>