Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一个列表中不在第二个列表中的值的Scala索引_Scala - Fatal编程技术网

一个列表中不在第二个列表中的值的Scala索引

一个列表中不在第二个列表中的值的Scala索引,scala,Scala,我试图在一个Scala列表中找到第二个列表中不存在的元素的索引(假设第二个列表有不同的元素,这样您就不需要调用来设置)。我发现最好的方法是: val ls = List("a", "b", "c") // the list val excl = List("c", "d") // the list of items to exclude val ixs = ls.zipWithIndex. filterNot{p => excl.contains(p._

我试图在一个Scala列表中找到第二个列表中不存在的元素的索引(假设第二个列表有不同的元素,这样您就不需要调用
来设置
)。我发现最好的方法是:

    val ls = List("a", "b", "c") // the list
    val excl = List("c", "d") // the list of items to exclude
    val ixs = ls.zipWithIndex.
      filterNot{p => excl.contains(p._1)}.
      map{ p => p._2} // the list of indices

不过,我觉得应该有一个更直接的方法。有什么提示吗?

我觉得没问题。作为一种理解方式,它可能更优雅一些:

for ((e,i) <- ls.zipWithIndex if !excl.contains(e)) yield i

我觉得还可以。作为一种理解方式,它可能更优雅一些:

for ((e,i) <- ls.zipWithIndex if !excl.contains(e)) yield i
一个想法是这样的

(ls.zipWithIndex.toMap -- excl).values
但是,仅当您对列表中某个元素多次出现时的所有位置都不感兴趣时才有效。这需要一个Scala标准库中没有的多重映射

另一个版本是使用分部函数,首先将第二个列表转换为集合(除非它非常小,否则在集合中查找将非常快)

一个想法是这样的

(ls.zipWithIndex.toMap -- excl).values
但是,仅当您对列表中某个元素多次出现时的所有位置都不感兴趣时才有效。这需要一个Scala标准库中没有的多重映射

另一个版本是使用分部函数,首先将第二个列表转换为集合(除非它非常小,否则在集合中查找将非常快)