如何从列表元素创建所有可能的组合? [英] How to create all possible combinations from the elements of a list?
本文介绍了如何从列表元素创建所有可能的组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
列表(a,b,c,d,e)
如何从上面的列表中创建所有可能的组合?
我期望的是:
$ $ $
$ $ $ $ >
解决方案
或者您可以使用子集
方法。不过,您必须先将列表转换为集合。
scala> List(1,2,3).toSet [Int] .subsets.map(_ .toList).toList
res9:List [List [Int]] = List(List(),List(1),List (2),清单(3),清单(1,2),清单(1,3),清单(2,3),清单(1,2,3))
I have the following list:
List(a, b, c, d, e)
How to create all possible combinations from the above list?
I expect something like:
a
ab
abc
解决方案
Or you could use the subsets
method. You'll have to convert your list to a set first though.
scala> List(1,2,3).toSet[Int].subsets.map(_.toList).toList
res9: List[List[Int]] = List(List(), List(1), List(2), List(3), List(1, 2), List(1, 3), List(2, 3), List(1, 2, 3))
这篇关于如何从列表元素创建所有可能的组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文