我有以下字母矢量:
my_alphs <- c("X","Y","Z")
给出一个带有星号(*)作为通配符的字符串:
my_str <- "LA**"
请注意,字符串的长度可以大于4,并且 星号的位置可以在任何具有不同长度的位置。
我想根据存储在my_alphs
中的字母枚举所有星号(*),结果是这样(我手动执行此操作):
LAXX
LAXY
LAXZ
LAYX
LAYY
LAYZ
LAZX
LAZY
LAZZ
如何用R实现呢?
答案 0 :(得分:3)
根据需要更改>>> [k*val for k, g in itertools.groupby(x) if sum(1 for _ in g) == val]
和my_str
。
my_alphs
答案 1 :(得分:1)
这是一个基本的R
解决方案,可以推广到*
的任意数量和位置
replace_wildcards <- function(str, alphs) {
strs <- strsplit(str, "")[[1]]
combs <- do.call(expand.grid, list(alphs)[rep(1, sum(strs == "*"))])
frame <- do.call(cbind, lapply(strs, rep, NROW(combs)))
frame[, strs == "*"] <- as.matrix(combs)
apply(frame, 1, paste, collapse = "")
}
示例:
replace_wildcards("LA**", c("X","Y","Z"))
# [1] "LAXX" "LAYX" "LAZX" "LAXY" "LAYY" "LAZY" "LAXZ" "LAYZ" "LAZZ"
replace_wildcards("*N*Y*", c("1", "2"))
# "1N1Y1" "2N1Y1" "1N2Y1" "2N2Y1" "1N1Y2" "2N1Y2" "1N2Y2" "2N2Y2"
replace_wildcards("**_is_here", c("Q", "I", "R"))
# [1] "QQ_is_here" "IQ_is_here" "RQ_is_here" "QI_is_here" "II_is_here" "RI_is_here" "QR_is_here" "IR_is_here" "RR_is_here"