不包括重复Excel的所有数字组合的总和

egdjgwm8  于 2023-05-08  发布在  其他
关注(0)|答案(2)|浏览(97)

这个最高投票的答案总和所有组合Excel或谷歌电子表格是接近我所需要的-但我需要一个解决方案,它保持组合的顺序,并排除重复基于此顺序。即1 2 3 4的数据集将显示以下的乘积:
1 1+2 1+3 1+4 1+2+3 1+2+4 1+3+4 1+2+3
二二三二四二三四
3 3+4
4

一二三四五六十
二五六
三七
4
在没有脚本的情况下,Google Sheets / Excel中是否可以做到这一点?
谢谢

1sbrub3j

1sbrub3j1#

这里有一个公式,可以按顺序显示总和

=ArrayFormula(sort(" "&transpose(split(SUBSTITUTE(substitute(textjoin("+",true,{if(mod(int(row(A1:A15)/(2^(column(A1:D1)-1))),2)=0,"",column(A1:D1)),B1:B15&";"}),"+;",";"),";+",";"),";")),1,true))

这个公式将按照它们生成的顺序给予总和,但是必须将它与第一个公式结合起来才能按照正确的顺序得到它们

=ArrayFormula(mmult(if(mod(int(row(A1:A15)/(2^(column(A1:D1)-1))),2)=0,0,column(A1:D1)),transpose(column(A1:D1))^0))

给这个

=ArrayFormula(sort({" "&transpose(split(SUBSTITUTE(substitute(textjoin("+",true,{if(mod(int(row(A1:A15)/(2^(column(A1:D1)-1))),2)=0,"",column(A1:D1)),B1:B15&";"}),"+;",";"),";+",";"),";")),ArrayFormula(mmult(if(mod(int(row(A1:A15)/(2^(column(A1:D1)-1))),2)=0,0,column(A1:D1)),transpose(column(A1:D1))^0))},1,true))

下面是使用实际值而不是列号时的外观

=ArrayFormula(sort({" "&transpose(split(SUBSTITUTE(substitute(textjoin("+",true,{if(mod(int(row(A1:A15)/(2^(column(A1:D1)-1))),2)=0,"",column(A1:D1)),if(row(A1:A15)>0,";")}),"+;",";"),";+",";"),";")),ArrayFormula(mmult(if(mod(int(row(A1:A15)/(2^(column(A1:D1)-1))),2)=0,0,A1:D1),transpose(column(A1:D1))^0))},1,true))

zqry0prt

zqry0prt2#

要获得真正的cross join or Cartesian product,请使用recursive lambda function,如下所示:

=let( 
  values, torow(A2:A, 1), 
  zeros, map(values, lambda(_, 0)), 
  table, { zeros; values }, 
  first_, lambda(array, tocol(choosecols(array, 1), true)), 
  rest_, lambda(n, choosecols(table, sequence(1, columns(table) - n, n + 1))), 
  wrap_, lambda(array, wrapCount, wraprows(tocol(array, 1), wrapCount)), 

  cartesian_, lambda(a, b, wrap_( 
    byrow(a, lambda(row, 
      reduce(iferror(ø), sequence(rows(b)), lambda(acc, i, 
        { acc, row, chooserows(b, i) } 
      ) ) 
    ) ), 
    columns(a) + columns(b) 
  ) ), 

  iterate_, lambda( 
    self, a, b, if(iserror(b), a, 
      self(self, cartesian_(a, first_(b)), rest_(columns(a) + 1)) 
    ) 
  ), 

  cartesian, iterate_(iterate_, first_(table), rest_(1)), 
  sums, byrow(cartesian, lambda(row, sum(row))), 
  expressions, byrow(cartesian, lambda(row, join(" + ", filter(row, row)))), 
  result, unique(filter({ expressions, sums }, sums)), 
  key, choosecols(result, 1), 
  sort(result, left(key), true, len(key), true, key, true) 
)

结果如下所示:
| 数据集|组合|总计|
| --------------|--------------|--------------|
| 1| 1| 1|
| 二|一加二|三|
| 三|1 + 3|四个|
| 四个|1 + 4|五|
| | 1 + 2 + 3|六|
| | 1 + 2 + 4|七|
| | 1 + 3 + 4|八|
| | 1 + 2 + 3 + 4| 10个|
| | 二|二|
| | 2 + 3|五|
| | 2 + 4|六|
| | 2 + 3 + 4|九|
| | 三|三|
| | 3 + 4|七|
| | 四个|四个|
要对任何值(如文本字符串或日期)执行相同的操作,请使用以下模式:

=let( 
  values, torow(filter(to_text(A2:A), len(A2:A))), 
  blanks, map(values, lambda(_, "")), 
  table, { blanks; values }, 
  first_, lambda(array, tocol(choosecols(array, 1), true)), 
  rest_, lambda(n, choosecols(table, sequence(1, columns(table) - n, n + 1))), 
  wrap_, lambda(array, wrapCount, wraprows(tocol(array, 1), wrapCount)), 

  cartesian_, lambda(a, b, wrap_( 
    byrow(a, lambda(row, 
      reduce(iferror(ø), sequence(rows(b)), lambda(acc, i, 
        { acc, row, chooserows(b, i) } 
      ) ) 
    ) ), 
    columns(a) + columns(b) 
  ) ), 

  iterate_, lambda( 
    self, a, b, if(iserror(b), a, 
      self(self, cartesian_(a, first_(b)), rest_(columns(a) + 1)) 
    ) 
  ), 

  cartesian, iterate_(iterate_, first_(table), rest_(1)), 
  expressions, byrow(cartesian, lambda(r, join(" + ", filter(r, len(r))))), 
  result, tocol(expressions, 3), 
  sort(result, left(result), true, len(result), true, result, true) 
)

结果如下所示:
| 数据集|组合|
| --------------|--------------|
| 一种|一种|
| B| a + B|
| c| a + c|
| d的|a + d|
| | a + B + c|
| | a + B + d|
| | a + c + d|
| | a + B + c + d|
| | B|
| | B + c|
| | B + d|
| | B + c + d|
| | c|
| | c + d|
| | d的|

相关问题