简体   繁体   English

scala过滤元组列表的列表

[英]scala filter a list of lists of tuples

I have a list of lists with tuples (char, Int) like 我有一个列表列表,其中包含元组(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)))`

I want to filter out all tuples with where the Int is 0. So the result should be: 我想过滤掉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)))

I tried to do a map and then a filter but the compiler complains with incompatible types: ((Int, Char) => Boolean expected but found ((Int, Char) => Unit) 我试图做一个地图,然后过滤器,但编译器抱怨incompatible types: ((Int, Char) => Boolean expected but found ((Int, Char) => Unit)

raw.map(_.filter(tuple => match {
  (_,0) => false
  (_,_) => true
})

Were do I go wrong 我出错了吗?

这是正确的:

raw.map(_.filter(_._2 != 0))

This is close to what you tried: 这接近你试过的:

raw.map(_.filter({
  case (_,0) => false
  case (_,_) => true
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM