KDB 中是否有等同于“inter”的“outer”或“exclusive or”运算符?
Is there an `outer` or `exclusive or` operator equivalent to `inter` in KDB?
假设我有以下两个列表:
x : `a`b`c`d;
y : `a`b`e`f;
对于交集,有 inter
运算符:
q)x inter y
`a`b
是否有类似的运算符来执行 EXCLUSIVE OR
这样我会得到:
q)x outer y
`c`d`e`f
?
操作 except
将为您提供一个列表中不属于另一个列表的元素。
但是在你的情况下 x except y
只会给出 `c`d
而 y except x
只会给出 `e`f
.
因此您可以使用其中之一;
q)(x except y),y except x
`c`d`e`f
或
q)(x union y) except (x inter y)
`c`d`e`f
或者不使用 except
q)where(count each group (distinct x), distinct y)=1
`c`d`e`f
如果你想得到所有独占元素的列表。
此致,凯文
根据 Kevin 的回答,第一个将给出重复项(如果它们存在于任一者中),最后一个将给出不同的列表。要按照您的要求使用函数中缀,您需要在 .q 命名空间
中定义该函数
q).q.outer:{(x union y) except (x inter y)}
q)x outer y
`c`d`e`f
假设我有以下两个列表:
x : `a`b`c`d;
y : `a`b`e`f;
对于交集,有 inter
运算符:
q)x inter y
`a`b
是否有类似的运算符来执行 EXCLUSIVE OR
这样我会得到:
q)x outer y
`c`d`e`f
?
操作 except
将为您提供一个列表中不属于另一个列表的元素。
但是在你的情况下 x except y
只会给出 `c`d
而 y except x
只会给出 `e`f
.
因此您可以使用其中之一;
q)(x except y),y except x
`c`d`e`f
或
q)(x union y) except (x inter y)
`c`d`e`f
或者不使用 except
q)where(count each group (distinct x), distinct y)=1
`c`d`e`f
如果你想得到所有独占元素的列表。
此致,凯文
根据 Kevin 的回答,第一个将给出重复项(如果它们存在于任一者中),最后一个将给出不同的列表。要按照您的要求使用函数中缀,您需要在 .q 命名空间
中定义该函数q).q.outer:{(x union y) except (x inter y)}
q)x outer y
`c`d`e`f