Python 重新定义数据库,使用映射和过滤器进行查询
我有一张“竞争”表。在第二级,我有跑步者,每个跑步者都有一个结果列表:Python 重新定义数据库,使用映射和过滤器进行查询,python,rethinkdb,Python,Rethinkdb,我有一张“竞争”表。在第二级,我有跑步者,每个跑步者都有一个结果列表: "total_results": { "433915": [ #runner_id { "judge": "15561671", "result": 5, "round": "1" }, { "judge": "08136a59", "result": 4, "round": "1" } ] } 我不想问: r
"total_results": {
"433915": [ #runner_id
{
"judge": "15561671",
"result": 5,
"round": "1"
},
{
"judge": "08136a59",
"result": 4,
"round": "1"
}
]
}
我不想问:
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))
这段代码运行良好。现在我想应用基于“舍入”值的过滤器。我将其添加到第二个.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()移到RequiredDB查询之外,然后像这样做:
by_round = filter(lambda x: x['round'] == round, results)
我得到了结果。
在重新思考问题时,我应该有点做错了。。。你能给我小费吗
p、 我在数据库中有数千个结果。基于我的查询参数,应该有几十个结果 我想你希望你的第二张
地图
成为一张concat\u地图
。(或者,如果您喜欢现有的输出格式,您可以将过滤器
放入映射
中,如.map(lambda x:x.filter(…)
)Hi@mlucy,非常感谢您的回复!是的,你是对的,用concat_地图替换我的第二张地图成功了。你能在你的答案中将concatMap替换为concatu映射吗(python方式)?我会把你的回答标为“接受”。