【问题标题】:How to pair up elements using permutations in an rdd list of lists如何使用列表的 rdd 列表中的排列来配对元素
【发布时间】:2022-12-18 16:07:24
【问题描述】:

我正在尝试从 rdd 中的每个列表列表中获取成对元素的列表。

我的数据:

[['a','b','c'],['e','f','g','h'],['x','y','z']]

我想 :

[('a','b'),('b','c'),('c','a'),('e','f'),('f','g'),('g','h'),('e','g'),('e','h')...... and all possible pairs]

【问题讨论】:

    标签: apache-spark permutation rdd python-itertools


    【解决方案1】:
    >>> data = [[['a','b','c'],['e','f','g','h'],['x','y','z']]]
    >>> df = spark.sparkContext.parallelize(data)
    >>> import itertools
    >>> df.map(lambda x: [list(itertools.combinations(i,2)) for i in x]).map(lambda x: list(itertools.chain.from_iterable(x))).foreach(lambda y: print(y))
    

    结果:

    [('a', 'b'), ('a', 'c'), ('b', 'c'), ('e', 'f'), ('e', 'g'), ('e', 'h'), ('f', 'g'), ('f', 'h'), ('g', 'h'), ('x', 'y'), ('x', 'z'), ('y', 'z')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2021-03-11
      • 2019-08-13
      • 1970-01-01
      • 2021-07-13
      相关资源
      最近更新 更多