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

    2. <small id='depZr'></small><noframes id='depZr'>

      使用 JSON 保存 Python 元组

      时间:2023-08-31
        <tbody id='YwAJN'></tbody>
      <tfoot id='YwAJN'></tfoot>

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

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

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

                本文介绍了使用 JSON 保存 Python 元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我对此还有些陌生,所以我可能不知道所有事物的常规术语:

                I'm still a little new to this, so I might not know all the conventional terms for things:

                使用 JSON 编码时是否可以保留 Python 元组?现在 json.loads(json.dumps(tuple)) 给了我一个列表.我不想将我的元组转换为列表,但我想使用 JSON.那么,有什么选择吗?

                Is it possible to preserve Python tuples when encoding with JSON? Right now json.loads(json.dumps(tuple)) gives me a list back. I don't want to convert my tuples to lists, but I want to use JSON. So, are there options?

                原因:我正在创建一个使用多维数组的应用程序,并不总是相同的形状.我有一些使用递归来探测数组并将端点转换为字符串或整数的类方法.我最近意识到(基于我的递归工作方式)我可以使用元组来防止对数组进行更深层次的递归搜索(Python rawks).在我确信我不需要深入探究我的数据结构的情况下,这可能会派上用场.

                The reason why: I'm creating an app that uses multi-dimensional arrays, not always the same shape. I've got some class methods that use recursion to probe the arrays and cast the endpoints as a string or int. I recently realized that (based on how my recursion works) I can use tuples to prevent deeper recursive searching of arrays (Python rawks). This could come in handy in situations where I know I for sure I won't need to be probing any deeper into my data structures.

                推荐答案

                你可以写一个高度专业化的encoder和decoder hook:

                You can write a highly-specialzed encoder and a decoder hook:

                import json
                
                class MultiDimensionalArrayEncoder(json.JSONEncoder):
                    def encode(self, obj):
                        def hint_tuples(item):
                            if isinstance(item, tuple):
                                return {'__tuple__': True, 'items': item}
                            if isinstance(item, list):
                                return [hint_tuples(e) for e in item]
                            if isinstance(item, dict):
                                return {key: hint_tuples(value) for key, value in item.items()}
                            else:
                                return item
                
                        return super(MultiDimensionalArrayEncoder, self).encode(hint_tuples(obj))
                
                def hinted_tuple_hook(obj):
                    if '__tuple__' in obj:
                        return tuple(obj['items'])
                    else:
                        return obj
                
                
                enc = MultiDimensionalArrayEncoder()
                jsonstring =  enc.encode([1, 2, (3, 4), [5, 6, (7, 8)]])
                
                print jsonstring
                
                # [1, 2, {"items": [3, 4], "__tuple__": true}, [5, 6, {"items": [7, 8], "__tuple__": true}]]
                
                print json.loads(jsonstring, object_hook=hinted_tuple_hook)
                
                # [1, 2, (3, 4), [5, 6, (7, 8)]]
                

                这篇关于使用 JSON 保存 Python 元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Python 的元组列表中对每个元组中的第一个值求和? 下一篇:Python - 将元组列表转换为字符串

                相关文章

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

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

                    <tfoot id='zXOEP'></tfoot>

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