gpt4 book ai didi

scala - Spark/Scala : Expand a list of (List[String], 字符串)元组

转载 作者:行者123 更新时间:2023-12-02 05:59:36 24 4
gpt4 key购买 nike

基本上this question仅适用于 Scala。

给定具有表单元素的 RDD,我如何进行以下转换

(List[String], String) => (String, String)

例如

([A,B,C], X)
([C,D,E], Y)

to

(A, X)
(B, X)
(C, X)
(C, Y)
(D, Y)
(E, Y)

所以

最佳答案

scala> val l = List((List('a, 'b, 'c) -> 'x), List('c, 'd, 'e) -> 'y)
l: List[(List[Symbol], Symbol)] = List((List('a, 'b, 'c),'x),
(List('c, 'd, 'e),'y))

scala> l.flatMap { case (innerList, c) => innerList.map(_ -> c) }
res0: List[(Symbol, Symbol)] = List(('a,'x), ('b,'x), ('c,'x), ('c,'y),
('d,'y), ('e,'y))

关于scala - Spark/Scala : Expand a list of (List[String], 字符串)元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31961245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com