这是我得到的一个示例对象:{ "query1" : [ { "name" : "John", "id" : 1234 }, { "name" : "Rose", "id" : 3214 }, { "name" : "John", "id" : 1234 } ] }如何使用group by和数组导航/取消装箱来删除重复项?我尝试在where子句后实现groupby子句,但没有得到正确答案
{ "query1" : [ { "name" : "John", "id" : 1234 }, { "name" : "Rose", "id" : 3214 }, { "name" : "John", "id" : 1234 } ] }
wn9m85ua1#
要从Json对象中删除重复项,可以使用以下代码:
from collections import OrderedDict data = { "query1": [ {"name": "John", "id": 1234}, {"name": "Rose", "id": 3214}, {"name": "John", "id": 1234}, ] } unique_data = {} for key, array in data.items(): unique_objects = OrderedDict() for obj in array: unique_objects[(obj["name"], obj["id"])] = obj unique_data[key] = list(unique_objects.values()) print(unique_data)
结果:
{'query1': [{'name': 'John', 'id': 1234}, {'name': 'Rose', 'id': 3214}]}
xxe27gdn2#
这样做是可能的,找到了答案here:
data = { "query1": [ {"name": "John", "id": 1234}, {"name": "Rose", "id": 3214}, {"name": "John", "id": 1234}, ] } query1 = data.get('query1') [dict(t) for t in {tuple(d.items()) for d in query1}]
2j4z5cfb3#
在JSONiq中,您确实可以使用group by和数组取消装箱来删除重复项,如下所示:
let $data := { "query1" : [ { "name" : "John", "id" : 1234 }, { "name" : "Rose", "id" : 3214 }, { "name" : "John", "id" : 1234 } ] } return { "query1" : [ for $obj in $data.query1[] group by $n := $obj.name, $i := $obj.id return $obj[1] ] }
还有一种通用的方法,即使是未知字段和嵌套较多的值也可以使用:
let $data := { "query1" : [ { "name" : "John", "id" : 1234 }, { "name" : "Rose", "id" : 3214 }, { "name" : "John", "id" : 1234 } ] } return { "query1" : [ for $obj at $i in $data.query1[] where every $other in $data.query1[][position() lt $i] satisfies not deep-equal($obj, $other) return $obj ] }
3条答案
按热度按时间wn9m85ua1#
要从Json对象中删除重复项,可以使用以下代码:
结果:
xxe27gdn2#
这样做是可能的,找到了答案here:
2j4z5cfb3#
在JSONiq中,您确实可以使用group by和数组取消装箱来删除重复项,如下所示:
还有一种通用的方法,即使是未知字段和嵌套较多的值也可以使用: