【问题标题】:RDD of integers to all possible pairs (ordered)所有可能对的整数 RDD(有序)
【发布时间】:2020-12-28 14:17:58
【问题描述】:

我正在使用 pyspark 从 int 数组的 RDD 中查找所有可能的对。

输入:

[[0, 1, 2],
[3, 4]]

输出所有可能组合的RDD键值对:

(0,1), (0,2), (1,0), ....  (3,4), (4,3)

我想在python而不是scala中实现它

【问题讨论】:

标签: python pyspark rdd


【解决方案1】:

permutations 可用于生成长度为 2 的元组。

from itertools import permutations

rdd = spark.sparkContext.parallelize([[0, 1, 2],[3, 4]])

rdd.flatMap(lambda x: permutations(x,2)).collect()

输出

[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (3, 4), (4, 3)]

【讨论】:

    猜你喜欢
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2019-07-19
    • 2021-03-27
    • 1970-01-01
    • 2016-02-25
    • 2021-08-27
    相关资源
    最近更新 更多