我有一个带有元组(char,Int)的列表列表,如
val raw = List(List((a,0), (b,0)), List((a,1), (b,0)), List((a,2), (b,0)), List((a,0), (b,1)), List((a,1), (b,1)), List((a,2), (b,1)), List((a,0), (b,2)), List((a,1), (b,2)), List((a,2), (b,2)))`
我想过滤掉Int为0的所有元组。所以结果应该是:
List(List(), List((a,1)), List((a,2)), List((b,1)), List((a,1), (b,1)), List((a,2), (b,1)), List((b,2)), List((a,1), (b,2)), List((a,2), (b,2)))
我尝试做一个地图然后过滤器,但编译器抱怨incompatible types: ((Int, Char) => Boolean expected but found ((Int, Char) => Unit)
raw.map(_.filter(tuple => match {
(_,0) => false
(_,_) => true
})
我出错了
答案 0 :(得分:5)
这是正确的:
raw.map(_.filter(_._2 != 0))
答案 1 :(得分:2)
这与你的尝试很接近:
raw.map(_.filter({
case (_,0) => false
case (_,_) => true
}))