替换列表中与KDB Q中的子字符串不匹配的元素
我如何遍历KDBQ中的列表并替换与某个子字符串标准不匹配的元素 逻辑示例伪代码:替换列表中与KDB Q中的子字符串不匹配的元素,kdb,Kdb,我如何遍历KDBQ中的列表并替换与某个子字符串标准不匹配的元素 逻辑示例伪代码: list.stream() .forEach(x -> { if (x matches substring) : newList.add(x) else : newList.add("") }) S: ("Lint"; "Stack"; "Linode"; "Overflow";"Linux") S: ("L
list.stream()
.forEach(x -> {
if (x matches substring) :
newList.add(x)
else :
newList.add("")
})
S: ("Lint"; "Stack"; "Linode"; "Overflow";"Linux")
S: ("Lint"; ""; "Linode"; "";"Linux")
当前列表:
list.stream()
.forEach(x -> {
if (x matches substring) :
newList.add(x)
else :
newList.add("")
})
S: ("Lint"; "Stack"; "Linode"; "Overflow";"Linux")
S: ("Lint"; ""; "Linode"; "";"Linux")
此处要匹配的子字符串是“Li”
。因此,字符串“Stack”和“Overflow”被空字符串替换,因为它们不包含子字符串
结果列表:
list.stream()
.forEach(x -> {
if (x matches substring) :
newList.add(x)
else :
newList.add("")
})
S: ("Lint"; "Stack"; "Linode"; "Overflow";"Linux")
S: ("Lint"; ""; "Linode"; "";"Linux")
你可以用几种方法来解决这个问题。首先使用条件求值对每个元素进行迭代,如下所示:
q){$[x like "Li*";x;" "]}each S
"Lint"
" "
"Linode"
" "
"Linux"
q)@[S;where not S like\: "Li*";:;" "]
"Lint"
" "
"Linode"
" "
"Linux"
或使用如下类似的方法:
q){$[x like "Li*";x;" "]}each S
"Lint"
" "
"Linode"
" "
"Linux"
q)@[S;where not S like\: "Li*";:;" "]
"Lint"
" "
"Linode"
" "
"Linux"
矢量条件也会起作用:
q)?[S like "Li*";S;count[S]#enlist ""]
"Lint"
""
"Linode"
""
"Linux"
我认为您希望将“
作为默认值,而不是”
,否则您将混合类型在第二种情况下不需要将每个左(\:)与like一起使用,因为like已经在列表中迭代。