【问题标题】:List concatenation not working in scala列表连接在scala中不起作用
【发布时间】:2018-03-21 15:40:59
【问题描述】:

我正在尝试使用以下代码在循环中连接 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) 

但是当我遍历最终列表时,它会给我单个列表而不是串联列表的单个元素。

以下是上述代码的O/P:

In Loop List(kroger 10TV DispatchAlerts)
In Loop List(kroger seanhannity SenTedCruz)
List element -> kroger seanhannity SenTedCruz 
List element -> kroger 10TV DispatchAlerts 

【问题讨论】:

  • 您创建了一个新的列表名称,这就是 List 元素循环分别打印每个元素的原因,您是否尝试将 lit 的每个元素组合成一个字符串?
  • 你的 ::: 功能正在做它应该做的事情,即连接到一个新列表中
  • 我必须通过在循环中组合从函数返回的列表来创建一个列表。
  • 奇怪的是控制台输出的前两行打印了List,但接下来的两行没有打印任何List,而是看起来像是String 的值用空格隔开...

标签: scala list scala-collections


【解决方案1】:

您的代码不是很实用,因为您正在改变变量。以下更优雅:

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)

其中“++”用于连接两个列表的元素。

【讨论】:

    【解决方案2】:

    原因是, 当您执行 names ::: List("anything") -> 时,它不会向名称添加任何内容。 相反,它会创建一个新集合。 例如,

    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)
    

    【讨论】:

    • 感谢您的回复。我使用 :::= 运算符(名称 :::= 影响者名称)连接到列表。我可以看到最终列表中的所有元素,但它们不会作为单个元素返回。
    • 不,面临同样的问题。
    • 能否请您编辑您的问题并发布您对方法所做的任何更改,以便其他人可以轻松发现问题。
    【解决方案3】:

    看起来问题出在 searchIndex 方法中,该方法使用单个字符串检索 List[String],其中包含由空格分隔的所有值,修复该方法以确保它检索每个值一个元素的 List .

    要检查这是否正确,试试这个,这只是一种解决方法,你应该修复 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) 
    

    【讨论】:

    • searchIndex 的返回类型是 List[String],我使用 ::: 连接 List,但它没有给我预期的 O/P。
    • 在 searchIndex 中使用以下代码创建列表: var result : List [String] = List() for (...) { val value : String = .. result = value :: result }
    • 感谢米克尔的回复。是的,更新对我有用
    猜你喜欢
    • 2017-04-23
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2011-09-27
    • 2014-06-07
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多