SimpleJSON和NumPy数组 [英] SimpleJSON and NumPy array

查看:101
本文介绍了SimpleJSON和NumPy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用simplejson序列化numpy数组的最有效方法是什么?

What is the most efficient way of serializing a numpy array using simplejson?

推荐答案

我会使用simplejson.dumps(somearray.tolist())作为最方便的 方法(如果我仍在使用simplejson的话)被Python 2.5或更早版本所卡住; 2.6和更高版本具有标准库模块json,该模块的工作方式相同,因此如果使用的Python版本支持它,我当然会使用它;-).

I'd use simplejson.dumps(somearray.tolist()) as the most convenient approach (if I was still using simplejson at all, which implies being stuck with Python 2.5 or earlier; 2.6 and later have a standard library module json which works the same way, so of course I'd use that if the Python release in use supported it;-).

为寻求更高的效率,您可以可以子类 json.JSONEncoder (在json中;我不知道较早的simplejson是否已经提供了这样的自定义可能性),并且在default方法中,通过旋转将它们按时"放入列表或元组.我有点怀疑您是否会通过这种方法在性能方面获得足够的利益来证明这种努力的合理性.

In a quest for greater efficiency, you could subclass json.JSONEncoder (in json; I don't know if the older simplejson already offered such customization possibilities) and, in the default method, special-case instances of numpy.array by turning them into list or tuples "just in time". I kind of doubt you'd gain enough by such an approach, in terms of performance, to justify the effort, though.

这篇关于SimpleJSON和NumPy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆