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

      1. <tfoot id='xC2Lw'></tfoot><legend id='xC2Lw'><style id='xC2Lw'><dir id='xC2Lw'><q id='xC2Lw'></q></dir></style></legend>

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

          <bdo id='xC2Lw'></bdo><ul id='xC2Lw'></ul>
      2. 为相同的字典值创建可交换元组键的最佳方法是什么?

        What#39;s the best way to create exchangeable tuple keys for the same dictionary value?(为相同的字典值创建可交换元组键的最佳方法是什么?)

        <legend id='ZDbnl'><style id='ZDbnl'><dir id='ZDbnl'><q id='ZDbnl'></q></dir></style></legend>
              <tbody id='ZDbnl'></tbody>

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

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

                • 本文介绍了为相同的字典值创建可交换元组键的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  def check():
                      dict_choice_a = {(a, b) : value, (b, a) : value}  #(a, b) and (b, a) refer to the same value but repeted
                      dict_choice_b = {tuple(sorted((a, b)) : value}  #not repetitive but unreadable
                      dict_choice_a[(a, b)] = new_value #need to do twice to change value but more readable than dict_choice_b
                      dict_choice_a[(b, a)] = new_value
                  
                      #value of both keys are always the same
                  

                  我想创建一个 dictionary 元组键引用其值,键需要可交换为 (a, b) = (b, a)并且它们只引用相同的值.

                  I want to create a dictionary that has tuple keys referred to its values, that keys need to be exchangeable as (a, b) = (b, a) and they only refer to the same value.

                  这里的问题是:使 tulpe of keys 的元素可交换但也引用相同值的最佳方法是什么.

                  Here's the question is: what is the best way to make the element of tulpe of keys exchangeable but also refer to the same value.

                  此外,字符串也应该在解决方案中起作用.

                  Moreover, string should be also work in the solution.

                  推荐答案

                  根据评论,您可以将 ab 放入一个 frozenset,这是无序的:

                  Per the comments, you can put a and b into a frozenset, which is unordered:

                  dict_choice = {frozenset((a, b)): value}
                  

                  <小时>

                  如果您需要它是自动的,您可以创建自己的 MutableMapping:

                  class MyDict(MutableMapping):
                  
                      def __init__(self, arg=None):
                          self._map = {}
                          if arg is not None:
                              self.update(arg)
                  
                      def __getitem__(self, key):
                          return self._map[frozenset(key)]
                  
                      def __setitem__(self, key, value):
                          self._map[frozenset(key)] = value
                  
                      def __delitem__(self, key):
                          del self._map[frozenset(key)]
                  
                      def __iter__(self):
                          return iter(self._map)
                  
                      def __len__(self):
                          return len(self._map)
                  

                  使用中:

                  >>> d = MyDict([((1, 2), 'hello'), ((3, 4), 'world')])
                  >>> d[(2, 1)]
                  'hello' 
                  

                  但是请注意,这可能会对其他类型的键产生意外行为:

                  However note that this could have unexpected behaviour with other kinds of keys:

                  >>> d['hello'] = 'world'
                  >>> d['hole']
                  'world'
                  >>> d[1] = 2
                  Traceback (most recent call last):
                    File "python", line 1, in <module>
                    File "python", line 14, in __setitem__
                  TypeError: 'int' object is not iterable
                  

                  这篇关于为相同的字典值创建可交换元组键的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

                      <bdo id='rae5K'></bdo><ul id='rae5K'></ul>
                        <tbody id='rae5K'></tbody>

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

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