如何将列表转换为Unlist?

时间:2016-09-14 02:26:33

标签: scala k-means

我希望'清楚' 列表所以,输出就像这样

(5,36,5,36,0.434, 0.36, 0.392, 0.37)
(1,28,1,28,0.457, 0.588, 0.66, 0.478, 0.358)
(1,41,1,41,0.381, 0.289, 0.433)
(5,37,5,37,0.525)

现在我有这样的输出

(5,36,5,36,List(0.434, 0.36, 0.392, 0.37))
(1,28,1,28,List(0.457, 0.588, 0.66, 0.478, 0.358))
(1,41,1,41,List(0.381, 0.289, 0.433))
(5,37,5,37,List(0.525))

我的代码是

val NEWMAPPINGFILE = pFile.map(_.split(",")).map
    {
      baris =>
        (
          baris(0).toInt,
          baris(1).toInt,
          baris(2).toInt,
          baris(3).toInt,
          vektorASCII(baris(4).split("@").toList)
        )
    }.foreach(println)

def vektorASCII(pArgument:List[String]) =
  {
    pArgument.map
    {
      _.map
      {
        _.toByte.toInt
      }.sum.toDouble / 1000
    }
  }

任何想法,我该怎么做?这可能吗?任何参考或链接或教程,将有所帮助...... 非常感谢

2 个答案:

答案 0 :(得分:1)

这应该有效:

library(data.table)
setDT(df1)[, lapply(.SD, uniqueN), by= c1]
#   c1 c2 c3
#1: 10  2  3
#2: 20  2  3

答案 1 :(得分:0)

只需添加,非常少的代码

vektorASCII(baris(4).split("@").toList).mkString(",")

输出就像我想要的那样:

(1,28,1,28,0.457,0.588,0.66,0.478,0.358)
(5,36,5,36,0.434,0.36,0.392,0.37)
(1,41,1,41,0.381,0.289,0.433)
(5,37,5,37,0.525)
谢谢你!