从 Python 创建 JSON 的更有效方法 [英] More efficient way to create JSON from Python

查看:81
本文介绍了从 Python 创建 JSON 的更有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 API,该 API 从磁盘上的 CSV(带有 x、y 坐标)读取并以 JSON 格式输出它们以供 Web 前端呈现.问题是有很多数据点(30k 的数量级),因此从 x 和 y 的 numpy 数组转换为 JSON 真的很慢.

I'd like to write an API that reads from a CSV on disk (with x, y coordinates) and outputs them in JSON format to be rendered by a web front end. The issue is that there are lots of data points (order of 30k) and so going from numpy arrays of x and y into JSON is really slow.

这是我当前获取 JSON 格式数据的函数.有没有办法加快这个速度?每个二维点都有这么大的数据结构似乎很多余.

This is my current function to get the data in JSON format. Is there any way to speed this up? It seems very redundant to have such a large data structure for each 2d point.

def to_json(xdata, ydata):
    data = []
    for x, y in zip(xdata, ydata):
        data.append({"x": x, "y": y})
    return data

推荐答案

你可以使用列表推导式:

You could use list comprehension like:

def to_json(xdata, ydata):
    return  [{"x": x, "y": y} for x, y in zip(xdata, ydata)]

消除使用不必要的变量,并且更干净.

Eliminates use of unnessacary variable, and is cleaner.

您还可以使用以下生成器:

You can also use generators like:

def to_json(xdata, ydata):
    return  ({"x": x, "y": y} for x, y in zip(xdata, ydata))

它们的创建速度非常快,并且对系统很轻,几乎不占用内存.直到您执行诸如将其转换为列表之类的操作.

They're created super fast and are light on the system, use little to no memory. This last's until you do something like convert it to a list.

由于对象只是 x-y 坐标,我会使用带有 x-y 元组的生成器对象 - 它们的创建速度也更快 - 像这样:

Since the objects are just x-y co-ordinates i'd use a generator object with x-y tuples - which are also created faster - like so:

def to_json(xdata, ydata):
    return  ((x,y) for x, y in zip(xdata, ydata))

您可以用列表 [] 替换元组,它们是有效的 JSON 数组.

You could replace the tuples with lists [], theyre valid JSON arrays.

这篇关于从 Python 创建 JSON 的更有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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