Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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

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
Algorithm 如何在scala中有效地将正规集[a]转换为元组集[(a,Int)]集?_Algorithm_Scala_Performance_Set_Tuples - Fatal编程技术网

Algorithm 如何在scala中有效地将正规集[a]转换为元组集[(a,Int)]集?

Algorithm 如何在scala中有效地将正规集[a]转换为元组集[(a,Int)]集?,algorithm,scala,performance,set,tuples,Algorithm,Scala,Performance,Set,Tuples,我有一个叫做picks的可变集合[a]。我想创建一个名为set的不可变集,它是一个元组。它的第一个元素是picks集合中的原始A,第二个元素是int,第一个元素是零。这是我的 var set = scala.collection.mutable.Set[(A, Int)]() for (index <- picks) yield (set += (index -> 0)) var set=scala.collection.mutable.set[(A,Int)]() (索引0))

我有一个叫做picks的可变集合[a]。我想创建一个名为set的不可变集,它是一个元组。它的第一个元素是picks集合中的原始A,第二个元素是int,第一个元素是零。这是我的

var set = scala.collection.mutable.Set[(A, Int)]()
for (index <- picks) yield (set += (index -> 0))
var set=scala.collection.mutable.set[(A,Int)]()
(索引0))
这段代码可以正常工作,并且符合我的要求。当布景变大时,它只是非常慢。有没有更快的方法

编辑

我的代码的这一部分也太慢了,需要改进

val temp = scala.collection.mutable.Set[(A, Int)]()
    for (index <- s) yield (if(index._2 != maxLies) {
      temp += (index._1 -> (index._2 + 1))
    })
val temp=scala.collection.mutable.Set[(A,Int)]()
用于(索引(索引2+1))
})

它创建一个临时集合,并通过一个已定义的集合s循环,该集合名为[(a,Int)],检查Int是否大于某个数字,如果大于,则将其从集合中删除。如果不是,那么它会向Int添加一个我认为
zipWithIndex
可以满足您的需要

var set = Set("a", "b", "c")
val tuples = set.zipWithIndex { case (item, index) => (item, index) } 

// ( ("a", 0), ("b", 1), ("c", 2)

val set=picks.map(->0)
picks
未更改,仍可用于查询。要从修改的
set
转换为新的不可变,您可以
val pix=set.map(u.\u 1).toSet
。您正在用Scala语言编写C代码。我建议花一点时间学习一个好的以FP为中心的教程以及Scala标准库。
val x=s.collect{case(a,b)if b!=maxLies=>(a,b+1)}