列出一起涵盖所有给定元素的字符串的所有组合

时间:2017-03-01 09:57:52

标签: r string combinations

说我得到以下字符串:

1:{a,b,c,t}
2:{b,c,d}
3:{a,c,d}
4:{a,t}

我想创建一个程序,它将为我提供这些字符串的所有不同组合,其中每个组合必须包含每个给定的字母。 因此,例如,上述组合是字符串{1& 2,1& 3,2& 3& 4,1& 2& 3& 4,2& 4}。

我正在考虑使用for循环执行此操作,程序将查看第一个字符串,找到缺少的元素,然后在列表中向下查找具有这些字母的字符串。但是我觉得这个想法只能找到两个字符串的组合,而且还需要列出所有字母到程序中,这看起来非常不经济。

2 个答案:

答案 0 :(得分:1)

我认为这样的事情应该有用。

sets <- list(c('a', 'b', 'c', 't'),
             c('b', 'c', 'd'),
             c('a', 'c', 'd'),
             c('a', 't'))

combinations <- lapply(2:length(sets),
                       function(x) combn(1:length(sets), x, simplify=FALSE))
combinations <- unlist(combinations, FALSE)
combinations
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] 1 3
# 
# [[3]]
# [1] 1 4
# 
# [[4]]
# [1] 2 3
# 
# [[5]]
# [1] 2 4
# 
# [[6]]
# [1] 3 4
# 
# [[7]]
# [1] 1 2 3
# 
# [[8]]
# [1] 1 2 4
# 
# [[9]]
# [1] 1 3 4
# 
# [[10]]
# [1] 2 3 4
# 
# [[11]]
# [1] 1 2 3 4

u <- unique(unlist(sets))
u
# [1] "a" "b" "c" "t" "d"

Filter(function(x) length(setdiff(u, unlist(sets[x]))) == 0, combinations)
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] 1 3
# 
# [[3]]
# [1] 2 4
# 
# [[4]]
# [1] 1 2 3
# 
# [[5]]
# [1] 1 2 4
# 
# [[6]]
# [1] 1 3 4
# 
# [[7]]
# [1] 2 3 4
# 
# [[8]]
# [1] 1 2 3 4

答案 1 :(得分:0)

首先...... 我有空的时候会编辑这个答案。以下结果取决于选择顺序。我还没弄明白如何压扁清单。如果我可以展平它,我会对每个结果进行排序,然后删除重复项。

v = list(c("a","b","c","t"),c("b","c","d"),c("a","c","d"),c("a","t"))

allChars <- Reduce(union, v) # [1] "a" "b" "c" "t" "d"

charInList <- function(ch, li) which(sapply(li, function(vect) ch %in% vect))
locations <- sapply(allChars, function(ch) charInList(ch, v) )
# > locations
# $a
# [1] 1 3 4
# 
# $b
# [1] 1 2
# 
# $c
# [1] 1 2 3
# 
# $t
# [1] 1 4
# 
# $d
# [1] 2 3

findStillNeeded<-function(chosen){
  haveChars <- Reduce(union, v[chosen]) 
  stillNeed <- allChars[!allChars %in% haveChars] 
  if(length(stillNeed) == 0 ) return(chosen) #terminate if you dont need any more characters
  return ( lapply(1:length(stillNeed), function(i) { #for each of the characters you still need
    loc <- locations[[stillNeed[i]]] #find where the character is located
    lapply(loc, function(j){
      findStillNeeded(c(chosen, j)) #when you add this location to the choices, terminate if you dont need any more characters
    }) 
  }) )

}

result<-lapply(1:length(v), function(i){
  findStillNeeded(i)
})