获取长度为N的字符串的所有组合

问题描述 投票:0回答:2

我有以下数据:

fruits <- c(
  "apples and oranges and pears and bananas",
  "pineapples and mangos and guavas"
)


str_split(fruits, " and ", simplify = TRUE)

我正在尝试获取长度可以为 1、2、3、4 等的变量/字符的所有可能组合

预期产出:

apples
apples + oranges
apples + oranges + pears
apples + oranges + pears + bananas

oranges
oranges + pears
oranges + pears + bananas

pears
pears + bananas
r string combinations
2个回答
2
投票

我们可以使用'rje'包中的

powerSet
函数并将输出包装在
lapply()
中以创建公式对象或字符串列表:

library(stringr)
library(rje)

fruits <- c(
  "apples and oranges and pears and bananas",
  "pineapples and mangos and guavas"
)

fruit_ls <- str_split(fruits, " and ") 

# formula objects
lapply(powerSet(fruit_ls[[1]])[-1], \(x) reformulate(x))

#> [[1]]
#> ~apples
#> <environment: 0x0000028529d9f218>
#> 
#> [[2]]
#> ~oranges
#> <environment: 0x0000028529d7dc20>
#> 
#> [[3]]
#> ~apples + oranges
#> <environment: 0x00000285296ceb68>
#> 
#> [[4]]
#> ~pears
#> <environment: 0x00000285296c4338>
#> 
#> [[5]]
#> ~apples + pears
#> <environment: 0x00000285296aeeb8>
#> 
#> [[6]]
#> ~oranges + pears
#> <environment: 0x000002852969cf78>
#> 
#> [[7]]
#> ~apples + oranges + pears
#> <environment: 0x00000285296979b0>
#> 
#> [[8]]
#> ~bananas
#> <environment: 0x0000028529685808>
#> 
#> [[9]]
#> ~apples + bananas
#> <environment: 0x0000028529694870>
#> 
#> [[10]]
#> ~oranges + bananas
#> <environment: 0x0000028529681468>
#> 
#> [[11]]
#> ~apples + oranges + bananas
#> <environment: 0x000002852968f2e0>
#> 
#> [[12]]
#> ~pears + bananas
#> <environment: 0x000002852968aee8>
#> 
#> [[13]]
#> ~apples + pears + bananas
#> <environment: 0x000002852966e0b8>
#> 
#> [[14]]
#> ~oranges + pears + bananas
#> <environment: 0x0000028529670950>
#> 
#> [[15]]
#> ~apples + oranges + pears + bananas
#> <environment: 0x0000028529652f98>


# strings
lapply(powerSet(fruit_ls[[1]])[-1], \(x) paste(x, collapse = " + "))

#> [[1]]
#> [1] "apples"
#> 
#> [[2]]
#> [1] "oranges"
#> 
#> [[3]]
#> [1] "apples + oranges"
#> 
#> [[4]]
#> [1] "pears"
#> 
#> [[5]]
#> [1] "apples + pears"
#> 
#> [[6]]
#> [1] "oranges + pears"
#> 
#> [[7]]
#> [1] "apples + oranges + pears"
#> 
#> [[8]]
#> [1] "bananas"
#> 
#> [[9]]
#> [1] "apples + bananas"
#> 
#> [[10]]
#> [1] "oranges + bananas"
#> 
#> [[11]]
#> [1] "apples + oranges + bananas"
#> 
#> [[12]]
#> [1] "pears + bananas"
#> 
#> [[13]]
#> [1] "apples + pears + bananas"
#> 
#> [[14]]
#> [1] "oranges + pears + bananas"
#> 
#> [[15]]
#> [1] "apples + oranges + pears + bananas"

创建于 2023-03-30 与 reprex v2.0.2


2
投票
fruits <- c(
  "apples and oranges and pears and bananas",
  "pineapples and mangos and guavas"
)

fruits_split <- str_split(fruits, " and ", simplify = TRUE)

for (i in 1:ncol(fruits_split)) {
  for (j in i:ncol(fruits_split)) {
    cat(paste(fruits_split[i:j], collapse = " + "), "\n")
  }
}

这就是你要找的吗? 内部循环遍历从 i 到 ncol(fruits_split) 的所有列,确保生成所有可能的水果组合,直到水果总数。

fruits <- c(
  "apples and oranges and pears and bananas and Pineapples and mangos and guavas"
)

fruits_split <- str_split(fruits, " and ", simplify = TRUE)

for (i in 1:ncol(fruits_split)) {
  for (j in i:ncol(fruits_split)) {
    cat(paste(fruits_split[i:j], collapse = " + "), "\n")
  }
}

输出:

apples 
apples + oranges 
apples + oranges + pears 
apples + oranges + pears + bananas 
apples + oranges + pears + bananas + Pineapples 
apples + oranges + pears + bananas + Pineapples + mangos 
apples + oranges + pears + bananas + Pineapples + mangos + guavas 
oranges 
oranges + pears 
oranges + pears + bananas 
oranges + pears + bananas + Pineapples 
oranges + pears + bananas + Pineapples + mangos 
oranges + pears + bananas + Pineapples + mangos + guavas 
pears 
pears + bananas 
pears + bananas + Pineapples 
pears + bananas + Pineapples + mangos 
pears + bananas + Pineapples + mangos + guavas 
bananas 
bananas + Pineapples 
bananas + Pineapples + mangos 
bananas + Pineapples + mangos + guavas 
Pineapples 
Pineapples + mangos 
Pineapples + mangos + guavas 
mangos 
mangos + guavas 
guavas
© www.soinside.com 2019 - 2024. All rights reserved.