【发布时间】:2011-05-23 12:22:24
【问题描述】:
考虑这样的地图:
Map("one" -> Iterable(1,2,3,4), "two" -> Iterable(3,4,5), "three" -> Iterable(1,2))
我想获取Iterable 下所有可能的元素排列列表,每个键对应一个元素。对于这个例子,这将是这样的:
// first element of "one", first element of "two", first element of "three"
// second element of "one", second element of "two", second element of "three"
// third element of "one", third element of "two", first element of "three"
// etc.
Seq(Iterable(1,3,1), Iterable(2,4,2), Iterable(3,5,1),...)
什么是实现这一目标的好方法?
【问题讨论】:
-
这不是排列。排列在所有可能的顺序中都是单个集合,例如 1、2、3; 1,3,2; 2,1,3; 2,3,1; 3,1,2; 3,2,1。您可能的意思是“转置”(第二个索引的元素优先于第一个索引)。
-
什么定义了键的顺序,因为 Map 键没有排序?
-
@Paul 我不想订购它。考虑
Iterable(1,2,3)、Iterable(2,3,1)、Iterable(3,1,2)和Iterable(3,2,1)是相同的。 -
所以您想将映射中的键视为无序,将每个值迭代器中的值视为无序,将结果视为一组无序的无序值(每个映射值中的一个)?只是检查,因为您的示例是有序的(因为它是 Seq)
标签: scala collections map