在类型为 (List,Integer) 的元组的 RDD 中展平列表

Flatten list within RDD of tuples with type (List,Integer)

我在 PySpark 中有一个格式为 (List,Integer) 的元组 RDD。

示例:

(["Hello","How","are","you"],12)

我想将其转换为

类型的RDD
("Hello",12),
("How",12),
("are",12),
("you",12)

您可以使用 flatMap:

rdd.collect()
# [(['Hello', 'How', 'are', 'you'], 12)]

rdd2 = rdd.flatMap(lambda r: [(i, r[1]) for i in r[0]])

rdd2.collect()
# [('Hello', 12), ('How', 12), ('are', 12), ('you', 12)]