scala 如何过滤scala中的元组列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13122432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to filter a list of tuples in scala?
提问by Shakti
I have the following list in Scala which I want to filter based on a criteria such that if there are any tuples with duplicate first value , Like from the below example I want to exclude
我在 Scala 中有以下列表,我想根据条件进行过滤,以便如果有任何元组具有重复的第一个值,就像下面的示例一样,我想排除
List((a,1), (a,2))
since it is having have "a" as duplicate .
因为它有“a”作为重复。
回答by pagoda_5b
You want to filter your resultwith a function that only leaves untouched those lists with no duplicate "keys"
你想result用一个函数来过滤你的,只留下那些没有重复“键”的列表
result filter noDuplicateKeys
This filtering function must take a list of pairs List[(A,B)]and return true only if the pair first element is not present elsewhere for all elements, and can be implemented as
此过滤函数必须采用对列表,List[(A,B)]并且仅当所有元素的对第一个元素不存在于其他地方时才返回 true,并且可以实现为
def noDuplicateKeys[A, B](xs: List[(A, B)]) =
(xs groupBy (_._1)).values forall {_.size < 2}
It groups the elements into a Mapwhose keys are the As and whose values are a corresponding list of the Bs paired with that key.
它将元素分组为 a,Map其键是As,其值是B与该键配对的s的对应列表。
From this values you can only accept those that have a size no more than 1
从这个值中,您只能接受大小不超过 1 的值
回答by Brian
I saw this as filtering out items from List[List[Tuple2[Char, Int]]]where an element of the list has the same elements. In this case an element of the list is List[Tuple2[Char, Int]]which I want to return a Booleanwhen the elements are the same. This List[Tuple2[Char, Int]]gets mapped to the first element of the Tupleand then logic is applied to the new list of chars using distinct and size to check if the elements are the same and to retain those that only have one element.
我认为这是从List[List[Tuple2[Char, Int]]]列表中的元素具有相同元素的地方过滤掉项目 。在这种情况下,列表的一个元素是List[Tuple2[Char, Int]]我想Boolean在元素相同时返回的元素。这List[Tuple2[Char, Int]]被映射到 的第一个元素,Tuple然后使用不同的和大小将逻辑应用于新的字符列表,以检查元素是否相同并保留那些只有一个元素的元素。
The filter
过滤器
filter(xs => xs.map(ys => ys._1).distinct.size != 1 || xs.size == 1)
Test data:
测试数据:
scala> val a = List(List(('a',1)))
a: List[List[(Char, Int)]] = List(List((a,1)))
scala> val aa = List(List(('a',1),('a',1)))
aa: List[List[(Char, Int)]] = List(List((a,1), (a,1)))
scala> val ab = List(List(('a',1),('b',1)))
ab: List[List[(Char, Int)]] = List(List((a,1), (b,1)))
scala> val aba = List(List(('a',1),('b',1),('a',1)))
aba: List[List[(Char, Int)]] = List(List((a,1), (b,1), (a,1)))
Test cases.
测试用例。
scala> a.filter(xs => xs.map(ys => ys._1).distinct.size != 1 || xs.size == 1)
res34: List[List[(Char, Int)]] = List(List((a,1)))
scala> aa.filter(xs => xs.map(ys => ys._1).distinct.size != 1 || xs.size == 1)
res35: List[List[(Char, Int)]] = List()
scala> ab.filter(xs => xs.map(ys => ys._1).distinct.size != 1 || xs.size == 1)
res36: List[List[(Char, Int)]] = List(List((a,1), (b,1)))
scala> aba.filter(xs => xs.map(ys => ys._1).distinct.size != 1 || xs.size == 1)
res37: List[List[(Char, Int)]] = List(List((a,1), (b,1), (a,1)))
回答by wleao
I believe that the following code will do what you want (to remove only the first pair, right?):
我相信下面的代码会做你想做的事(只删除第一对,对吧?):
I'm using pattern matching to do so, if you want to filter everything, u could do a recursion over your list or do what Kyle proposed. Check this out:
我正在使用模式匹配来这样做,如果您想过滤所有内容,您可以对您的列表进行递归或执行 Kyle 建议的操作。看一下这个:
val a = "a"
val b = "b"
var result =
List(
List(),
List((a, 1), (a, 2)),
List((a, 1), (b, 1)),
List((a, 1), (b, 2)),
List((a, 2), (b, 1)),
List((a, 2), (b, 2)),
List((b, 1), (b, 2)),
List((a, 1), (a, 2), (b, 1)),
List((a, 1), (a, 2), (b, 2)),
List((a, 1), (b, 1), (b, 2)),
List((a, 2), (b, 1), (b, 2)),
List((a, 1), (a, 2), (b, 1), (b, 2)))
val filteredResult = (for (list <- result)
yield list match {
case x :: y :: xys if (x._1 == y._1) => xys
case _ => list
}).distinct
Result:
结果:
//> List()
//| List((a,1), (b,1))
//| List((a,1), (b,2))
//| List((a,2), (b,1))
//| List((a,2), (b,2))
//| List((b,1))
//| List((b,2))
//| List((a,1), (b,1), (b,2))
//| List((a,2), (b,1), (b,2))
//| List((b,1), (b,2))
The distinct will just filter the resulting empty lists.
distinct 只会过滤生成的空列表。
Cheers!
干杯!
回答by Jules Ivanic
based on the @pagoda_5b solution, I think we could find a better one.
基于@pagoda_5b 解决方案,我认为我们可以找到更好的解决方案。
What do you think about:
你有什么想法:
def noDuplicateKeys(l: List[(Char, Int)]): Boolean = l.toMap.size == l.size
list filter noDuplicateKeys
;)
;)

![scala 如何找到两个 Option[Int] 的 min() 或 max()](/res/img/loading.gif)