如何将 Pyspark 最短路径距离作为整数返回?

问题描述

我正在使用 Pyspark 的内置 shortestPaths 函数来返回从每个顶点到给定目的地的最短路径。但是,它以 [1 -> 0] 格式返回,如下所示在我的输出中。我怎样才能让它以整数形式返回单个距离,所以在 [1 -> 0] 的情况下它只会返回 0?

我的代码如下:

def get_shortest_distances(graphframe,dst_id):

    result = graphframe.shortestPaths(landmarks=[dst_id])
    result.show()

g = GraphFrame(vertices,edges)

get_shortest_distances(g,'1')

输出

+---+---------+
| id|distances|
+---+---------+
|  1| [1 -> 0]|
|  3| [1 -> 2]|
|  2| [1 -> 1]|
|  4|       []|
|  5|       []|
|  0| [1 -> 1]|
+---+---------+

所需的输出

+---+---------+
| id|distances|
+---+---------+
|  1|       0 |
|  3|       2 |
|  2|       1 |
|  4|    none |
|  5|    none |
|  0|       1 |
+---+---------+

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)