scala列表zipwithindex的不同结果?

7d7tgy0s  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(412)
val df = Seq(

        (1, "A, B, C, D"), 
        (2, "E, F, G,5"), 
        (3, "H, I,5,7"), 
        (4, "J,sd,cc,23")
).toDF("number","letters")
df.show()
output:
+------+----------+
|number|   letters|
+------+----------+
|     1|A, B, C, D|
|     2| E, F, G,5|
|     3|  H, I,5,7|
|     4|J,sd,cc,23|
+------+----------+
val arr = df.filter($"number"===1).select($"letters").head.toSeq
val list1 = arr.toList
list1.zipWithIndex

//为什么没有列表((“a”,0),(“b”,1),(“c”,2),(“d”,3))?

output:
arr: Seq[Any] = WrappedArray(A, B, C, D)
list1: List[Any] = List(A, B, C, D)
res87: List[(Any, Int)] = List((A, B, C, D,0))

然而

List("a", "b", "c").zipWithIndex
output:
    res88: List[(String, Int)] = List((a,0), (b,1), (c,2))

为什么它们都有不同输出格式的zipwithindex?

ssgvzors

ssgvzors1#

当你打电话的时候 .head 在Dataframe上返回 Row .
如果你看看 list1 您将看到它只包含一个元素。
您需要提取行的元素0处的字符串:

df
  .filter($"number"===1)
  .select($"letters")
  .head
  .getString(0)
  .split(",")
  .zipWithIndex

相关问题