gpt4 book ai didi

有条件地重复字符串的某些部分

转载 作者:行者123 更新时间:2023-12-02 02:31:22 25 4
gpt4 key购买 nike

我想在 ] 之间重复字符串的某些部分和;作为由 ; 分隔的元素数量[] 内的前面。因此所需的输出为 [A1, AB11; A2, AB22] I1, C1将是[A1, AB11] I1, C1; [A2, AB22] I1, C1 。任何开始的提示。谢谢

df1 <-
data.frame(
String = c(
"[A1, AB11; A2, AB22] I1, C1; [A3, AB33] I3, C1"
, "[A4, AB44] I4, C4; [A5, AB55; A6, AB66; A7, AB77] I7, C7"
)
)
df1

String
1 [A1, AB11; A2, AB22] I1, C1; [A3, AB33] I3, C1
2 [A4, AB44] I4, C4; [A5, AB55; A6, AB66; A7, AB77] I7, C7


df2 <-
data.frame(
String = c(
"[A1, AB11] I1, C1; [A2, AB22] I1, C1; [A3, AB33] I3, C1"
, "[A4, AB44] I4, C4; [A5, AB55] I7, C7;[A6, AB66] I7, C7; [A7, AB77] I7, C7"
)
)

df2

String
1 [A1, AB11] I1, C1; [A2, AB22] I1, C1; [A3, AB33] I3, C1
2 [A4, AB44] I4, C4; [A5, AB55] I7, C7;[A6, AB66] I7, C7; [A7, AB77] I7, C7

最佳答案

这是一个基本的 R 解决方案:

sapply(strsplit(paste0(df1$String, ";"), "\\[|\\]"), function(x) {
for(i in seq_along(x))
{
if(i %% 2 == 0) {
x[i] <- paste0("[", gsub(";", paste0("]", x[i + 1], " ["), x[i]), "]")
}
}
paste(x, collapse = "")
})
#> [1] "[A1, AB11] I1, C1; [ A2, AB22] I1, C1; [A3, AB33] I3, C1;"
#> [2] "[A4, AB44] I4, C4; [A5, AB55] I7, C7; [ A6, AB66] I7, C7; [ A7, AB77] I7, C7;"

关于有条件地重复字符串的某些部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64993095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com