gpt4 book ai didi

python - list_value 不能包含包含另一个 list_value 的 Value

转载 作者:行者123 更新时间:2023-12-04 07:23:33 25 4
gpt4 key购买 nike

我有一个二维数字矩阵,我想将它存储到云数据存储中,

from google.cloud import datastore
client = datastore.Client(project='my-random-project')

complete_key = client.key("Task", "2185e0c5")
task = datastore.Entity(key=complete_key, exclude_from_indexes=['data'])

task.update({'data': {'input': [[1,2,3],[4,5,6],[7,8,9]], 'output': [[2,3,4],[5,6,7],[8,9,10]]}})

client.put(task)
所以当我运行上面的代码时,我收到以下错误
InvalidArgument: 400 list_value cannot contain a Value containing another list_value.
怎么把这种数据放到 cloud-datastore ?

最佳答案

快速而肮脏的解决方案:转储到 json 并存储为字符串:

import json
data_raw = {
'input': [[1,2,3],[4,5,6],[7,8,9]],
'output': [[2,3,4],[5,6,7],[8,9,10]]
}
task.update({'data': json.dumps(data_raw)})

You'll need to configure data to accept either stringValue (will not allow utilization of values in db side filtering/querying).


更好的解决方案是将每个值作为索引存储在对象中:
data_raw = {
'input': {
1: [1,2,3],
2: [4,5,6],
3: [7,8,9]
},
'output': {
1: [2,3,4],
2: [5,6,7],
3: [8,9,10]
}
}

I'm not 100% sure how to map the entity type here, but you do get a compatible though highly nested object.If ordering matters here but you cannot anticipate ordering ahead of time (eq to "on insert, set index"), consider using some hash algorithm to store an ordered index. This is probably a specialized use case which will take a ton of time to develop.

关于python - list_value 不能包含包含另一个 list_value 的 Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68351642/

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