【问题标题】:found String, Required (String,String,String,Int): tuples-scala找到 String, 必需 (String,String,String,Int): tuples-scala
【发布时间】:2017-05-03 23:25:31
【问题描述】:

我有 3 个长度相等的 ListBuffer。

设备名称列表:ListBuffer[字符串]

datelist:ListBuffer[String]

wordcountssortedlistbuf[(String,Int)]

现在我需要转换它们的格式

ListBuffer(String,String,String,Int)

我尝试了以下操作

 var sortedrecords=scala.collection.mutable.ListBuffer[(String,String,String,Int)]()

 for(i <- 0 to devicenamelist.length)
{

sortedrecords+=(devicenamelist(i),datelist(i),wordcountssortedlistbuf(i)._1,wordcountssortedlistbuf(i)._2)  

}

它给我的错误如下

[错误] 找到字符串

必需(字符串、字符串、字符串、整数)

当我打算创建 (String,String,String,Int) 时,顶部的列表附加操作如何只给出一个字符串。我错过了什么吗?

谢谢

【问题讨论】:

    标签: scala list apache-spark tuples


    【解决方案1】:

    您在 += 行中缺少一组括号,但是,请不要那样做,看到有人在 scala 中写这样的东西会伤害我的眼睛。

    试试这样的:

    val sortedrecords = devicenamelist.zip(datelist).zip(wordcountssortedlistbuf)
     .map { case ((devicename, date), (word, count)) => 
       (devicename, date, word, count)
     }
    

    【讨论】:

    • @KevinMeredith 在 OP 的代码中伤害我眼睛的不是元组运算符,而是命令式循环填充的可变缓冲区。但是,元组运算符、过于具体的类型以及对列表的索引访问也无济于事......
    猜你喜欢
    • 2015-04-11
    • 1970-01-01
    • 2021-10-16
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2021-09-28
    相关资源
    最近更新 更多