用 Scala 替换 List 中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5062435/
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
Replace element in List with scala
提问by Berlin Brown
How do you replace an element by index with an immutable List.
如何用不可变列表按索引替换元素。
E.g.
例如
val list = 1 :: 2 ::3 :: 4 :: List()
list.replace(2, 5)
回答by Rex Kerr
If you want to replace index 2, then
如果要替换索引 2,则
list.updated(2,5) // Gives 1 :: 2 :: 5 :: 4 :: Nil
If you want to find every place where there's a 2 and put a 5 in instead,
如果你想找到每一个有 2 的地方,然后把 5 放在里面,
list.map { case 2 => 5; case x => x } // 1 :: 5 :: 3 :: 4 :: Nil
In both cases, you're not really "replacing", you're returning a new list that has a different element(s) at that (those) position(s).
在这两种情况下,您并不是真正地“替换”,而是返回一个新列表,该列表在该(那些)位置具有不同的元素。
回答by Vasil Remeniuk
In addition to what has been said before, you can use patchfunction that replaces sub-sequences of a sequence:
除了之前所说的,您还可以使用patch替换序列的子序列的函数:
scala> val list = List(1, 2, 3, 4)
list: List[Int] = List(1, 2, 3, 4)
scala> list.patch(2, Seq(5), 1) // replaces one element of the initial sequence
res0: List[Int] = List(1, 2, 5, 4)
scala> list.patch(2, Seq(5), 2) // replaces two elements of the initial sequence
res1: List[Int] = List(1, 2, 5)
scala> list.patch(2, Seq(5), 0) // adds a new element
res2: List[Int] = List(1, 2, 5, 3, 4)
回答by Ken Bloom
You can use list.updated(2,5)(which is a method on Seq).
您可以使用list.updated(2,5)(这是一种方法Seq)。
It's probably better to use a scala.collection.immutable.Vectorfor this purpose, becuase updates on Vectortake (I think) constant time.
scala.collection.immutable.Vector为此目的使用 a 可能更好,因为更新Vector需要(我认为)恒定时间。
回答by damon-lin
You can use map to generate a new list , like this :
您可以使用 map 生成一个新列表,如下所示:
@ list
res20: List[Int] = List(1, 2, 3, 4, 4, 5, 4)
@ list.map(e => if(e==4) 0 else e)
res21: List[Int] = List(1, 2, 3, 0, 0, 5, 0)
回答by nambastha
It can also be achieved using patch function as
也可以使用补丁函数来实现
scala> var l = List(11,20,24,31,35)
l: List[Int] = List(11, 20, 24, 31, 35)
scala> l.patch(2,List(27),1)
res35: List[Int] = List(11, 20, 27, 31, 35)
where 2 is the position where we are looking to add the value, List(27)is the value we are adding to the list and 1 is the number of elements to be replaced from the original list.
其中 2 是我们要添加值的位置,List(27)是我们添加到列表中的值,1 是要从原始列表中替换的元素数。
回答by praxnet
following is a simple example of String replacement in scala List, you can do similar for other types of data
下面是scala List中字符串替换的简单例子,其他类型的数据也可以做类似的操作
scala> val original: List[String] = List("a","b")
original: List[String] = List(a, b)
scala> val replace = original.map(x => if(x.equals("a")) "c" else x)
replace: List[String] = List(c, b)
回答by v6ak
If you do a lot of such replacements, it is better to use a muttable class or Array.
如果你做很多这样的替换,最好使用可变类或数组。

