我正在尝试在网格中对序列的元素进行分组。我有81个元素。
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 |
| 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 |
| 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 |
| 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 |
| 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
| 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 |
我想像这样将它们分成3x3的网格
| 0 | 1 | 2 |
| 9 | 10 | 11 |
| 18 | 19 | 20 |
我正在对此进行测试,我有一个输入
val input = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80"
然后我解析该输入
val parsed = input.split(",").toList.map { x => x.trim }
然后我将元素分组
val groups = parsed.grouped(3).toList
如果执行以下行,我将获得第一列网格
val grids1 = groups.sliding(1,3).toList.grouped(3).toList.flatten.flatten.grouped(3).toList
但是我不知道如何获得其他专栏
答案 0 :(得分:1)
假设您的输入中包含以下列表:
val input = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80)
然后您可以执行:
input.zipWithIndex
.map { case (d, i) => (i % 9 / 3, i / 27, d) } // 28 => (0, 1, 28)
.groupBy { case (x, y, d) => (x, y) }
.map { case ((x, y), values) => values.map(_._3) }
返回:
List(List(54, 55, 56, 63, 64, 65, 72, 73, 74), List(0, 1, 2, 9, 10, 11, 18, 19, 20), List(6, 7, 8, 15, 16, 17, 24, 25, 26), List(30, 31, 32, 39, 40, 41, 48, 49, 50), List(60, 61, 62, 69, 70, 71, 78, 79, 80), List(27, 28, 29, 36, 37, 38, 45, 46, 47), List(57, 58, 59, 66, 67, 68, 75, 76, 77), List(33, 34, 35, 42, 43, 44, 51, 52, 53), List(3, 4, 5, 12, 13, 14, 21, 22, 23))
流水线每个步骤的详细信息:
0
至2
变成main column 1
,列3
)来准备其聚类使用5
(例如main column 2
)到i % 9 / 3
变成39 % 9 / 3 = 1
,...),使用i / 27
({{1 }}。groupBy
对每个元素进行“主列/行”(x,y)组合。39 / 27 = 1
信息,只保留了最后一个(x, y)
的值。不确定所需的顺序,但是您可以使用最终的map
。
答案 1 :(得分:1)
object solution extends App{
val input = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, " +
"28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55," +
" 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80"
val inputList = input.split(",").toList.grouped(9).toList
.map(_.grouped(3).toList)
.transpose.flatMap(_.grouped(3).toList)
inputList.foreach(println)
}
我认为您正在寻找这个。
//output:
List(List(List(0, 1, 2), List( 9, 10, 11), List( 18, 19, 20)), List(List( 27, 28, 29), List( 36, 37, 38), List( 45, 46, 47)), List(List( 54, 55, 56), List( 63, 64, 65), List( 72, 73, 74)))
List(List(List( 3, 4, 5), List( 12, 13, 14), List( 21, 22, 23)), List(List( 30, 31, 32), List( 39, 40, 41), List( 48, 49, 50)), List(List( 57, 58, 59), List( 66, 67, 68), List( 75, 76, 77)))
List(List(List( 6, 7, 8), List( 15, 16, 17), List( 24, 25, 26)), List(List( 33, 34, 35), List( 42, 43, 44), List( 51, 52, 53)), List(List( 60, 61, 62), List( 69, 70, 71), List( 78, 79, 80)))
如果您想要这样的输出:
List(0, 1, 2, 9, 10, 11, 18, 19, 20)
List( 27, 28, 29, 36, 37, 38, 45, 46, 47)
List( 54, 55, 56, 63, 64, 65, 72, 73, 74)
List( 3, 4, 5, 12, 13, 14, 21, 22, 23)
List( 30, 31, 32, 39, 40, 41, 48, 49, 50)
List( 57, 58, 59, 66, 67, 68, 75, 76, 77)
List( 6, 7, 8, 15, 16, 17, 24, 25, 26)
List( 33, 34, 35, 42, 43, 44, 51, 52, 53)
List( 60, 61, 62, 69, 70, 71, 78, 79, 80)
您可以使用:
inputList.map(_.flatten).foreach(println)
答案 2 :(得分:0)
这可以通过在sliding
上使用List
函数来轻松完成
scala> input.sliding(9, 9).map(_.sliding(3,3).toList).foreach(println)
List(List(0, 1, 2), List(3, 4, 5), List(6, 7, 8))
List(List(9, 10, 11), List(12, 13, 14), List(15, 16, 17))
List(List(18, 19, 20), List(21, 22, 23), List(24, 25, 26))
List(List(27, 28, 29), List(30, 31, 32), List(33, 34, 35))
List(List(36, 37, 38), List(39, 40, 41), List(42, 43, 44))
List(List(45, 46, 47), List(48, 49, 50), List(51, 52, 53))
List(List(54, 55, 56), List(57, 58, 59), List(60, 61, 62))
List(List(63, 64, 65), List(66, 67, 68), List(69, 70, 71))
List(List(72, 73, 74), List(75, 76, 77), List(78, 79, 80))