如何在 Scala 中找到列表中的唯一项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1538598/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:41:09  来源:igfitidea点击:

How in Scala to find unique items in List

scala

提问by Volodymyr Bezuglyy

How in Scala to find unique items in List?

如何在 Scala 中找到列表中的唯一项?

采纳答案by Daniel Spiewak

The most efficient order-preserving way of doing this would be to use a Setas an ancillary data structure:

最有效的保持顺序的方法是使用 aSet作为辅助数据结构:

def unique[A](ls: List[A]) = {
  def loop(set: Set[A], ls: List[A]): List[A] = ls match {
    case hd :: tail if set contains hd => loop(set, tail)
    case hd :: tail => hd :: loop(set + hd, tail)
    case Nil => Nil
  }

  loop(Set(), ls)
}

We can wrap this in some nicer syntax using an implicit conversion:

我们可以使用隐式转换将其包装为更好的语法:

implicit def listToSyntax[A](ls: List[A]) = new {
  def unique = unique(ls)
}

List(1, 1, 2, 3, 4, 5, 4).unique    // => List(1, 2, 3, 4, 5)

回答by jamesqiu

In 2.8, it's:

在 2.8 中,它是:

List(1,2,3,2,1).distinct  // => List(1, 2, 3)

回答by Synesso

Roll your own uniq filter with order retention:

使用订单保留滚动您自己的 uniq 过滤器:

scala> val l = List(1,2,3,3,4,6,5,6)
l: List[Int] = List(1, 2, 3, 3, 4, 6, 5, 6)

scala> l.foldLeft(Nil: List[Int]) {(acc, next) => if (acc contains next) acc else next :: acc }.reverse
res0: List[Int] = List(1, 2, 3, 4, 6, 5)

回答by VonC

If you refer to the Rosetta Code: Create a Sequence of unique elements

如果您参考Rosetta Code: Create a Sequence of unique elements

val list = List(1,2,3,4,2,3,4,99)
val l2 = list.removeDuplicates
// l2: scala.List[scala.Int] = List(1,2,3,4,99)

Since Listis immutable, you wont modify the initial Listby calling removeDuplicates

由于List是不可变的,因此您不会List通过调用来修改初始值removeDuplicates

Warning: as mentioned by this tweet(!), this does not preserve the order:

警告:正如这条推文(!)所提到的,这不会保留顺序:

scala> val list = List(2,1,2,4,2,9,3)
list: List[Int] = List(2, 1, 2, 4, 2, 9, 3)

scala> val l2 = list.removeDuplicates
l2: List[Int] = List(1, 4, 2, 9, 3)

For a Seq, that method should be available in Scala2.8, according to ticket 929.
In the meantime, you will need to define an ad-hoc static method as the one seen here

对于 a Seq,根据票证 929,该方法应该在 Scala2.8 中可用。
同时,您需要定义一个 ad-hoc 静态方法,如此处 所示

回答by user unknown

Imho, all the interpretations of the question are false:

恕我直言,对问题的所有解释都是错误的:

How in Scala to find unique items in List?

如何在 Scala 中找到列表中的唯一项?

Given this list:

鉴于此列表:

val ili = List (1, 2, 3, 4, 4, 3, 1, 1, 4, 1) 

the only unique item in the list is 2. The other items aren't unique.

列表中唯一的唯一项是2。其他项目不是唯一的。

ili.toSet.filter (i => ili.indexOf (i) == ili.lastIndexOf (i))

will find it.

会找到的。

回答by slouc

list.filter { x => list.count(_ == x) == 1 }

回答by Mitch Blevins

A simple ad-hoc method is just to add the List to a Set, and use from there:

一个简单的临时方法就是将列表添加到集合中,然后从那里使用:

  val l = List(1,2,3,3,3,4,5,5,6,7,8,8,8,9,9)
  val s = Set() ++ x
  println(s)

Produces:

产生:

> Set(5, 1, 6, 9, 2, 7, 3, 8, 4)

This works for a Seq (or any Iterable), but is not necessary in 2.8, where the removeDuplicates method will probably be more readable. Also, not sure about the runtime performance vs a more thought-out conversion.

这适用于 Seq(或任何 Iterable),但在 2.8 中不是必需的,因为 removeDuplicates 方法可能更具可读性。此外,不确定运行时性能与更深思熟虑的转换。

Also, note the lost ordering.

另外,请注意丢失的顺序。

回答by Murilo

list.toSet will do it since Set by definition only contains unique elements

list.toSet 会这样做,因为 Set 根据定义只包含唯一元素