斯卡拉:比较所有元素在一个巨大的列表 [英] Scala: Compare all elements in a huge list
问题描述
请建议算法实现在一个很长的名单在斯卡拉比较的元素。我有成千上万的(从SQL)字符串列表,我需要每个列表元素在此列表中的所有其他元素进行比较。
Please advice on algorithm and implementation to compare elements in a very long list in Scala. I have a list with thousands of strings (from SQL) and I need to compare each list element with all other elements in this list.
因此,我需要得到一个元组列表:列表[(字符串,字符串,布尔值)]
,其中前两个元素是字符串匹配,三是的结果。
As a result I need to get a list of tuples: List[(String, String, Boolean)]
where first two elements are strings to match and third is a result.
有关N个元素的列表,我的算法迄今如下:
For a list of N elements my algorithm so far is as follows:
- 以头列表
- 与剩余的N-1个元素的列表比较头
- 请新的列表,从旧名单的尾巴,做所有上面的N-1的元素这个新的列表的工作:
code:
/**
* Compare head of the list with each remaining element in this list
*/
def cmpel(
fst: String, lst: List[String],
result: List[(String, String, Boolean)]): List[(String, String, Boolean)] = {
lst match {
case next :: tail => cmpel(fst, tail, (fst, next, fst == next) :: result)
case nill => result.reverse
}
}
/**
* Compare list elements in all combinations of two
*/
def cmpAll(lst: List[String],
result: List[(String, String, Boolean)]): List[(String, String, Boolean)] = {
lst match {
case head :: tail => cmpAll(tail, result ++ cmpel(head, tail, List()))
case nill => result
}
}
def main(args: Array[String]): Unit = {
val lst = List[String]("a", "b", "b", "a")
println(cmpAll(lst, List()))
}
结果:
List((a,b,false), (a,b,false), (a,a,true), (b,b,true), (b,a,false), (b,a,false))
谢谢!
推荐答案
您可以使用尾
和 flatMap
方法写一个更简洁,地道的解决方案:
You can use the tails
and flatMap
methods to write a more concise and idiomatic solution:
list.tails.flatMap {
case x :: rest => rest.map { y =>
(x, y, x == y)
}
case _ => List()
}.toList
在尾
方法返回一个迭代的迭代器 .tail
的反复应用到列表中。在迭代的第一个元素是列表本身,列表的再尾巴,等等,最后返回的空列表。
The tails
method returns an iterator that iterates over repeated applications of .tail
to the list. The first element in the iterator is the list itself, then the tail of the list, and so on, finally returning the empty list.
这篇关于斯卡拉:比较所有元素在一个巨大的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!