gpt4 book ai didi

python - RethinkDB,使用 map 和过滤器进行查询

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:19 25 4
gpt4 key购买 nike

我有rethinkdb“竞争”表。在第二级,我有运行者,每个运行者都有一个结果列表:

"total_results": {
"433915": [ #runner_id
{
"judge": "15561671",
"result": 5,
"round": "1"
},
{
"judge": "08136a59",
"result": 4,
"round": "1"
}
]
}

我进行 rethinkdb 查询:

results = (r.table('competitions').filter(lambda c: c['unique_id'] == competition_id)
.map(lambda c: c['total_results'])
.map(lambda t: t[runner_id])
.run(conn))

这段代码工作正常。现在我想应用基于“round”值的过滤器。我将其添加到第二个 .map() 之后,因此结果查询如下所示:

results = (r.table('competitions').filter(lambda c: c['unique_id'] == competition_id)
.map(lambda c: c['total_results'])
.map(lambda t: t[runner_id])
.filter(lambda x: x['round'] == round)
.run(conn))

但我得到的是空列表。这对我来说很奇怪,因为如果我将 .filter() 移到 rethinkdb 查询之外并执行以下操作:

by_round = filter(lambda x: x['round'] == round, results)

我得到了结果。在重新思考查询时我应该做错了什么......你能给我一个提示吗?

附:我的数据库中有数千个结果。根据我的查询参数应该有几十个结果。

最佳答案

我认为您希望您的第二个map是一个concat_map。 (或者,如果您喜欢现有的输出格式,则可以将 filter 放入 map 中,例如 .map(lambda x: x.filter(... ))。)

关于python - RethinkDB,使用 map 和过滤器进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820991/

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