Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.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
Scala 如何根据数组的第二个值对Seq[Array[(Int,Int)])进行分组_Scala - Fatal编程技术网

Scala 如何根据数组的第二个值对Seq[Array[(Int,Int)])进行分组

Scala 如何根据数组的第二个值对Seq[Array[(Int,Int)])进行分组,scala,Scala,我有一个序列Seq[(1,0),(2,0),(2,0),(1,1),(1,1),(2,1)] 我想将其修改为按第二个值分组的Seq[(1,2,2),(1,1,2)] 数组中每个贴图的 我试过.groupBy(u._2),但不起作用。它给了我 value _2 is not a member of scala.collection.immutable.Array[(Int,Int)] 有什么想法吗 试试这个 val input = Seq((1,0),(2,0),(2,0),(1,1),(1,1

我有一个序列
Seq[(1,0),(2,0),(2,0),(1,1),(1,1),(2,1)]

我想将其修改为按第二个值分组的
Seq[(1,2,2),(1,1,2)]

数组中每个贴图的

我试过
.groupBy(u._2)
,但不起作用。它给了我

value _2 is not a member of scala.collection.immutable.Array[(Int,Int)]
有什么想法吗

试试这个

val input = Seq((1,0),(2,0),(2,0),(1,1),(1,1),(2,1))
input.groupBy(_._2).collect{
  case e => e._2.map(_._1)
}
//res3: scala.collection.immutable.Iterable[Seq[Int]] = List(List(1, 1, 2), List(1, 2, 2))

如果您有
scala.collection.immutable.Array[(Int,Int)]
您只需要为group选择第二个值,然后只选择第一个值

data.groupBy(_._2).mapValues(x => x.map(_._1)).map(_._2).toList 
这将为您提供
列表[Array[Int]]

希望这有帮助