我对列表的组合有疑问:
我想我从输入中读取变量N(用户设置此变量)
然后我有以下列表:
val listExample = List("N1", "N2", "N4", "N5")
如果变量 N 3 ,那么我的输出将是 listExample 的可能组合(不重复),其中包含3个元素:< / p>
List("N1", "N2", "N4")
List("N1", "N2", "N5")
List("N1", "N4", "N5")
List("N2", "N4", "N5")
如果变量 N 2 ,那么我的输出将是 listExample 的可能组合(不重复),其中包含2个这样的元素:< / p>
List("N1", "N2")
List("N1", "N4")
List("N1", "N5")
List("N2", "N4")
List("N2", "N5")
List("N4", "N5")
我怎样才能做到这一点?
答案 0 :(得分:2)
找到答案:
listExample.combinations(n).toList