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

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

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

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

        JSON序列化以元组为键的字典

        时间:2023-09-01

            <tbody id='rFepz'></tbody>
          • <bdo id='rFepz'></bdo><ul id='rFepz'></ul>

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

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

                <legend id='rFepz'><style id='rFepz'><dir id='rFepz'><q id='rFepz'></q></dir></style></legend>
                  <tfoot id='rFepz'></tfoot>
                  本文介绍了JSON序列化以元组为键的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中有没有办法序列化使用元组作为键的字典?

                  Is there a way in Python to serialize a dictionary that is using a tuple as key?

                  例如

                  a = {(1, 2): 'a'}
                  

                  只需使用 json.dumps(a) 就会引发此错误:

                  simply using json.dumps(a) raises this error:

                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/usr/lib/python2.6/json/__init__.py", line 230, in dumps
                      return _default_encoder.encode(obj)
                    File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
                      chunks = list(self.iterencode(o))
                    File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
                      for chunk in self._iterencode_dict(o, markers):
                    File "/usr/lib/python2.6/json/encoder.py", line 268, in _iterencode_dict
                      raise TypeError("key {0!r} is not a string".format(key))
                  TypeError: key (1, 2) is not a string
                  

                  推荐答案

                  你不能将它序列化为 json,json 对于什么是 dict 键的理解远不如 python 灵活.

                  You can't serialize that as json, json has a much less flexible idea about what counts as a dict key than python.

                  您可以将映射转换为一系列键值对,如下所示:

                  You could transform the mapping into a sequence of key, value pairs, something like this:

                  import json
                  def remap_keys(mapping):
                      return [{'key':k, 'value': v} for k, v in mapping.iteritems()]
                  ... 
                  json.dumps(remap_keys({(1, 2): 'foo'}))
                  >>> '[{"value": "foo", "key": [1, 2]}]'
                  

                  这篇关于JSON序列化以元组为键的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用列表推导将元组的元组转换为一维列表? 下一篇:Python中方括号和括号括起来的列表有什么区别?

                  相关文章

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

                      <small id='436RI'></small><noframes id='436RI'>

                    2. <tfoot id='436RI'></tfoot>