gpt4 book ai didi

google-app-engine - 如何在 Google App Engine 数据存储中存储多维数组

转载 作者:太空宇宙 更新时间:2023-11-03 15:33:12 24 4
gpt4 key购买 nike

class Matrix(db.Model):
values = db.ListProperty()

obj = Matrix()

wx = [[1,0],[0,1]]

obj.put()

如何在数据存储中存储 wx 矩阵?

最佳答案

您需要序列化您的矩阵。你应该如何序列化数据取决于你是否要根据矩阵中的数据进行查询。

如果您不打算查询,只需使用 JSON(或类似的东西)。

from django.utils import simplejson as json

class Matrix(db.Model):
values = db.StringProperty(indexed=False)

matrix = Matrix()
# will be a string like: '[[1, 0], [0, 1]]'
matrix.values = json.dumps([[1,0],[0,1]])
matrix.put()

# To get back to the matrix:
matrix_values = json.loads(matrix.values)

如果您要尝试查询包含“精确行”的矩阵,那么您可能需要执行如下操作:

class Matrix(db.Model):
values = db.ListProperty()

matrix = Matrix()
values = [[1,0],[0,1]]
# will be a list of strings like: ['1:0', '0:1']
matrix.values = [':'.join([str(col) for col in row]) for row in values]
matrix.put()

# To get back to the matrix:
matrix_values = [[int(col) for col in row.split(':')] for row in matrix.values]

关于google-app-engine - 如何在 Google App Engine 数据存储中存储多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4357313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com