微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何将 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 举报,一经查实,本站将立刻删除。