我想从一组列表中的每个值创建一个元组列表。该列表可以打开,但是对于这个例子,我有以下三个字符串列表。如何从一组列表中的每个值创建元组列表
L1: (one, two three)
L2: (a, b, c)
L3: (yes, no)
我想返回一个元组列表,其中每个元组中的元素来自每个列表。在这种情况下,我将有18种组合(3 x 3 x 2)
T1: (one, a, yes)
T2: (one, a, no)
T3: (one, b, yes)
T4: (one, b, no)
T5: (one, c, yes)
T6: (one, c, no)
T7: (two, a, yes)
等等。在这种情况下,我们使用Java。
List<List<String>> list = getInput();
List<List<String> tuples = combinations(list);
其中getInput()返回我的输入(L1,L2,L3),以及它们的组合创建我的输出(T1,T2,T3 ......)
它被称为[笛卡尔积(http://en.wikipedia.org/wiki/Cartesian_product),和一个类似的问题了[问这里最近(HTTP:/ /stackoverflow.com/questions/7022500/java-how-to-build-unique-object-tuples-n-dimension)。那里也有一些很好的答案。 –
啊,当然......如果我在我的问题中使用笛卡儿,我肯定会找到答案 - 这是他们所有人的最佳答案。 – andyczerwonka