2016-05-15 94 views
0

我有Scala中兩個陣列都用相同的數字映射在階多個陣列

val v = myGraph.vertices.collect.map(_._1) 

其給出:

Array[org.apache.spark.graphx.VertexId] = Array(-7023794695707475841, -44591218498176864, 757355101589630892, 21619280952332745) 

和另一

val w = myGraph.vertices.collect.map(_._2._2) 

其給出:

Array[String] = Array(2, 3, 1, 2) 

,我想創建一個使用

val z = v.map("{id:" + _ + "," + "group:" + "1" + "}").mkString(",") 

這給出了一個字符串:

String = {id:-7023794695707475841,group:1},{id:-44591218498176864,group:1},{id:757355101589630892,group:1},{id:21619280952332745,group:1} 

但現在不是「1」的硬編碼組,我想,而不是從數字地圖w陣列給:

String = {id:-7023794695707475841,group:2},{id:-44591218498176864,group:3},{id:757355101589630892,group:1},{id:21619280952332745,group:2} 

我該如何做?

回答

1

在Scala集合中有一個名爲zip的方法,它可以根據需要配對兩個集合。

val v = Array(-37581, -44864, 757102, 21625) 
val w = Array(2, 3, 1, 2) 

val z = v.zip(w).map { 
    case (v, w) => "{id:" + v + "," + "group:" + w + "}" 
}.mkString(",") 

z變爲:

{id:-37581,group:2},{id:-44864,group:3},{id:757102,group:1},{id:21625,group:2} 
+0

Slouc,感謝您的答覆快和工作:) –