概述
函数difference()
计算在一个列表中存在,但在另一个列表中不存在的元素构成的列表。类似差集,但允许有重复元素。
参数:difference(<list_1>, <list_2>)
<list_1>
和<list_2>
:两个列表(list类型)
返回值:
<list_1>
中有但<list_2>
中没有的元素构成的列表(list类型)
一般用法
本例使用异源别名计算:
uncollect [[1,2,2],[2,4,5]] as a
uncollect [[2,4,7],[4,5,7]] as b
return table(toString(a), toString(b), toString(difference(a, b)))
| toString(a) | toString(b) | toString(difference(a, b)) |
|-------------|-------------|----------------------------|
| [1,2,2] | [2,4,7] | [1,2] |
| [2,4,5] | [4,5,7] | [2] |
本例将异源别名做笛卡尔积组合后计算:
uncollect [[1,2,2],[2,4,5]] as a
uncollect [[2,4,7],[4,5,7]] as b
with difference(a, b) as c
return table(toString(a), toString(b), toString(c))
| toString(a) | toString(b) | toString(c) |
|-------------|-------------|-------------|
| [1,2,2] | [2,4,7] | [1,2] |
| [1,2,2] | [4,5,7] | [1,2,2] |
| [2,4,5] | [2,4,7] | [5] |
| [2,4,5] | [4,5,7] | [2] |
示例图集:
![](https://img.ultipa.cn/draw/draw_fd8656adf7c843908a8228941a425401.jpg?v='')
在一个空图集中,依次运行以下各行语句创建示例图集:
create().node_schema("student").node_schema("course")
create().node_property(@*, "name").node_property(@student, "age", int32).node_property(@course, "credit", int32)
insert().into(@student).nodes([{_id:"S001", _uuid:1, name:"Jason", age:25}, {_id:"S002", _uuid:2, name:"Lina", age:23}, {_id:"S003", _uuid:3, name:"Eric", age:24}, {_id:"S004", _uuid:4, name:"Emma", age:26}, {_id:"S005", _uuid:5, name:"Pepe", age:24}])
insert().into(@course).nodes([{_id:"C001", _uuid:6, name:"French", credit:4}, {_id:"C002", _uuid:7, name:"Math", credit:5}])
insert().into(@default).edges([{_uuid:1, _from_uuid:1, _to_uuid:6}, {_uuid:2, _from_uuid:2, _to_uuid:6}, {_uuid:3, _from_uuid:3, _to_uuid:6}, {_uuid:4, _from_uuid:2, _to_uuid:7}, {_uuid:5, _from_uuid:3, _to_uuid:7}, {_uuid:6, _from_uuid:4, _to_uuid:7}, {_uuid:7, _from_uuid:5, _to_uuid:7}])
本例查找选择了法语课但没选择数学课的学生:
khop().src({name == "French"}).depth(1) as n1
with collect(n1) as l1
khop().src({name == "Math"}).depth(1) as n2
with collect(n2) as l2
return difference(l1, l2)
[
{"id":"","uuid":"1","schema":"student","values":{}}
]