Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create all possible combinations from the elements of a list?

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  
like image 736
Shakti Avatar asked Oct 28 '12 14:10

Shakti


People also ask

How do you generate all possible combinations of two lists in Python?

The unique combination of two lists in Python can be formed by pairing each element of the first list with the elements of the second list. Method 1 : Using permutation() of itertools package and zip() function. Approach : Import itertools package and initialize list_1 and list_2.

How do you find all combinations of a list excel?

Joining queries to find all combinations of two lists Once the queries from the tables are ready, go to Data > Get Data > Combine Queries > Merge to open the Merge dialog of Power Query. Select each table in the drop downs. Click on the column for each table to select them.


2 Answers

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)) 
like image 83
Kim Stebel Avatar answered Oct 31 '22 19:10

Kim Stebel


def combine(in: List[Char]): Seq[String] =      for {         len <- 1 to in.length         combinations <- in combinations len     } yield combinations.mkString  
like image 34
pagoda_5b Avatar answered Oct 31 '22 19:10

pagoda_5b