Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Scala中的列表?_List_Scala - Fatal编程技术网

List Scala中的列表?

List Scala中的列表?,list,scala,List,Scala,我有一个网格(声明为列表(列表)),每个3*3块被索引为0,下一个3*3块被索引为1,等等 我需要编写一个方法,返回给定索引的特定块中的元素(整数) 例如:b(0)应该给1 0 0 4 1 2 7 0 0 1 0 0 | 4 0 2 | 5 0 7 | 4 1 2 | 0 0 3 | 6 8 0 | 7 0 0 | 0 0 9 | 0 0 1 | ---------------------- 1 0 0 | 4 0 2 | 5 0 7 | 9 0 2 | 0 4 3 | 1 8 0 | 7 1

我有一个网格(声明为
列表(列表)
),每个
3*3
块被索引为
0
,下一个
3*3
块被索引为
1
,等等

我需要编写一个方法,返回给定索引的特定块中的元素(
整数)

例如:b(0)应该给1 0 0 4 1 2 7 0 0

1 0 0 | 4 0 2 | 5 0 7 |
4 1 2 | 0 0 3 | 6 8 0 |
7 0 0 | 0 0 9 | 0 0 1 |
----------------------
1 0 0 | 4 0 2 | 5 0 7 |
9 0 2 | 0 4 3 | 1 8 0 |
7 1 0 | 0 8 9 | 8 0 0 |
----------------------
1 0 0 | 4 0 2 | 5 0 7 |
4 0 2 | 0 1 9 | 6 2 0 |
7 0 0 | 0 0 9 | 0 0 1 |
----------------------

尝试以下功能

def block(i:Int, grid:List[List[Int]]) = grid.
  grouped(3).
  toList(i / 3).
  map(_.grouped(3).toList(i % 3))
下面是一个使用尼斯打印方法的示例

def print(q:List[List[Int]]) = q.map(_.mkString("\t")).mkString("\n")

val grid = (1 to 81).toList.grouped(9).toList

scala> print(block(1))
res37: String = 
   4    5   6
   13   14  15
   22   23  24
(对不起,我懒得写一个数独板作为例子:))

要获取整数列表,请尝试
flattern

scala> block(1).flattern
res38: List[Int] = List(4, 5, 6, 13, 14, 15, 22, 23, 34)

请给出一个明确的例子。
|x | o |
|x | o |
o | o | |
(分别为3行),这里的x表示所选的块(大小为2),o表示其余的项。@flavian请参见问题中的编辑谢谢你能解释一下吗?感谢lotsend使用指针发送完整的编译错误。
scala> block(1).flattern
res38: List[Int] = List(4, 5, 6, 13, 14, 15, 22, 23, 34)