列表连接在scala中不起作用

列表连接在scala中不起作用,scala,list,scala-collections,Scala,List,Scala Collections,我试图使用下面的代码在循环中连接scala列表 var names: List[String] = Nil val cluster_id = List("149095311_0", "149095311_1") for (id <- cluster_id) { val influencers_name = searchIndex(s"id : $id", "id", "influencers", searcher) println("In Loop " + influencers_

我试图使用下面的代码在循环中连接scala列表

var names: List[String] = Nil
val cluster_id = List("149095311_0", "149095311_1")
for (id <- cluster_id) {
  val influencers_name = searchIndex(s"id : $id", "id", "influencers", searcher)
  println("In Loop " + influencers_name)
  names :::= influencers_name
}
for(n <- names) println("List element -> " + n) 

问题似乎出在searchIndex方法中,该方法使用单个字符串检索列表[String],该字符串包含由空格分隔的所有值,请修复该方法以确保它检索每个值包含一个元素的列表

若要检查这是否正确,请尝试此操作,这只是一个解决方法,您应该修复searchIndex

var names: List[String] = Nil
val cluster_id = List("149095311_0", "149095311_1")
for (id <- cluster_id) {
  val influencers_name = searchIndex(s"id : $id", "id", "influencers", searcher).flatMap(_.split(' '))
  ("In Loop " + influencers_name)
  names = influencers_name ::: names
}
for(n <- names) println("List element -> " + n) 
var name:List[String]=Nil
val cluster_id=List(“149095311_0”、“149095311_1”)
因为(id原因是,
当您使用名称时:::List(“任何内容”)->它不会向名称添加任何内容。
相反,它会创建一个新集合。
比如说,

scala> var names: List[String] = Nil
names: List[String] = List()

scala> names ::: List("mahesh")
res0: List[String] = List(mahesh)

You can achive that

scala> names ::: List("chand")
res1: List[String] = List(chand)

scala> res0 ::: List("chand")
res2: List[String] = List(mahesh, chand)
当我向它添加“Mahesh”时,它创建了一个名为res0的新集合。 当我再次添加不同的字符串时,这里的“chand”已经创建了另一个集合。但是当我向创建的集合添加“chand”时,它已经连接到正确的集合

你可以实现你想做的事

scala> for(i <- List("a" ,"b" )){
     | names = i :: names } 

scala> names
res11: List[String] = List(b, a)
scala>用于(i)名称
res11:List[String]=List(b,a)

您的代码功能性不强,因为您正在变异变量。以下更为优雅:

def searchIndex(s: String): List[String] = {
  if (s == "149095311_0") List("kroger 10TV DispatchAlerts")
  else List("kroger seanhannity SenTedCruz")
}

val cluster_id = List("149095311_0", "149095311_1")

val names = cluster_id.foldLeft(List[String]()) {
  (acc, id) => acc ++ searchIndex(id)
}

for(n <- names) println("List element -> " + n)
def searchIndex(s:String):List[String]={
如果(s==“149095311_0”)列表(“克罗格10TV调度警报”)
其他列表(“kroger seanhannity SenTedCruz”)
}
val cluster_id=List(“149095311_0”、“149095311_1”)
val names=cluster_id.foldLeft(列表[字符串]()){
(acc,id)=>acc++搜索索引(id)
}

对于(n您创建了一个新的列表名称,这就是为什么列表元素循环单独打印每个元素,您是否尝试将lit的每个元素组合成一个字符串?您的:::功能正在做它应该做的事情,即连接到一个新列表。我必须通过组合循环中函数的列表返回来创建一个列表。奇怪的是,fir控制台输出的st 2行打印一个
列表
,但接下来的2行没有打印任何
列表
,相反,它看起来像是一个由空格分隔的值组成的
字符串…searchIndex的返回类型是List[String],我已经使用::::连接了列表,但它没有给我预期的O/P。在searchIndex列表中,使用以下代码创建:var result:List[String]=List(),for(…){val value:String=…result=value::result}谢谢Mikel的回复。是的,更新适用于您的回复。我正在使用:::=操作员(姓名::=影响者\ U姓名)连接到列表。我可以看到最终列表中的所有元素,但它们不是作为单个元素返回的。不,面临相同的问题。请编辑您的问题并发布您对方法所做的任何更改,以便其他人能够轻松发现问题。
def searchIndex(s: String): List[String] = {
  if (s == "149095311_0") List("kroger 10TV DispatchAlerts")
  else List("kroger seanhannity SenTedCruz")
}

val cluster_id = List("149095311_0", "149095311_1")

val names = cluster_id.foldLeft(List[String]()) {
  (acc, id) => acc ++ searchIndex(id)
}

for(n <- names) println("List element -> " + n)