list 如何从只有索引的 Scala 列表中删除项目?

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

How to remove an item from a list in Scala having only its index?

listscalafilterscala-2.10

提问by YoBre

I have a list as follows:

我有一个列表如下:

val internalIdList: List[Int] = List()

internalIdList = List(11, 12, 13, 14, 15)

From this list would remove the third element in order to obtain:

从这个列表中删除第三个元素以获得:

internalIdList = List(11, 12, 14, 15)

I can not use a ListBuffer, are obliged to maintain the existing structure. How can I do?

我不能使用一个ListBuffer,有义务维护现有的结构。我能怎么做?

Thanks to all

谢谢大家

回答by Rok Kralj

There is a .patchmethod on Seq, so in order to remove the third element you could simply do this:

有一个.patch方法Seq,所以为了删除第三个元素,你可以简单地这样做:

List(11, 12, 13, 14, 15).patch(2, Nil, 1)

Which says:Starting at index 2, please remove 1element, and replace it with Nil.

其中说:从索引2开始,请删除1 个元素,并将其替换为Nil

Knowing this method in depth enables you to do so much more than that. You can swap out any sublist of a list with arbitrary other.

深入了解这种方法可以让您做的远不止这些。您可以将列表的任何子列表替换为任意其他子列表。

回答by Shadowlands

Simply use

只需使用

val trunced = internalIdList.take(index) ++ internalIdList.drop(index + 1)

This will also work if index is larger than the size of the list (It will return the same list).

如果索引大于列表的大小,这也将起作用(它将返回相同的列表)。

回答by Nicolas

An idiomatic way to do it is to zip the value with their index, filter, and then project the value again:

一种惯用的方法是使用索引压缩值,过滤,然后再次投影该值:

scala> List(11,12,13,14,15).zipWithIndex.filter(_._2 != 2).map(_._1)
res0: List[Int] = List(11, 12, 14, 15)

But you can also use splitAt:

但您也可以使用splitAt

scala> val (x,y) = List(11,12,13,14,15).splitAt(2)
x: List[Int] = List(11, 12)
y: List[Int] = List(13, 14, 15)

scala> x ++ y.tail
res5: List[Int] = List(11, 12, 14, 15)

回答by Rok Kralj

If you insist on using the oldschool method, use collect:

如果您坚持使用 oldschool 方法,请使用 collect:

List(1,2,3,4).zipWithIndex.collect { case (a, i) if i != 2 => a }

However, I still prefer the method in my other answer.

但是,我仍然更喜欢我另一个答案中的方法。

回答by itsbruce

(internalIdList.indices.collect { case i if i != 3 => internalList(i) }).toList

To generalise this...

概括这...

def removeIndex[A](s: Seq[A], n: Int): Seq[A] = s.indices.collect { case i if i != n => s(i) }

Although this will often return a Vector, so you would need to do

虽然这通常会返回一个向量,所以你需要做

val otherList = removeIndex(internalIdList, 3).toList

If you really wanted a list back.

如果你真的想要一个列表。

Shadowlands has a solution which tendsto be faster for linear sequences. This one will be faster with indexed sequences.

Shadowlands 有一个解决方案,它对于线性序列往往更快。使用索引序列会更快。

回答by simleo

A generic function that implements Nicolas' first solution:

实现 Nicolas 的第一个解决方案的通用函数:

def dropIndex[T](list: List[T], idx: Int): List[T] =
  list.zipWithIndex.filter(_._2 != idx).map(_._1)

Usage:

用法:

scala> val letters = List('a', 'b', 'c')
scala> for (i <- 0 until letters.length) println(dropIndex(letters, i))
List(b, c)
List(a, c)
List(a, b)

回答by elm

Using a for comprehension on a list xslike this,

在这样的列表上使用 for comprehension xs

for (i <- 0 until xs.size if i != nth-1) yield xs(i)

Also consider a set of exclusion indices, for instance val excl = Set(2,4)for excluding the second and fourth items; hence we collect those items whose indices do not belong to the exclusion set, namely

还要考虑一组排除指数,例如val excl = Set(2,4)排除第二项和第四项;因此我们收集那些索引不属于排除集的项目,即

for (i <- 0 until xs.size if !excl(i)) yield xs(i)