如何将键值对简化为键和值列表? [英] How can a reduce a key value pair to key and list of values?
本文介绍了如何将键值对简化为键和值列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们假设,我在Spark中有一个键值对,如下所示.
Let us Assume, I have a key value pair in Spark, such as the following.
[ (Key1, Value1), (Key1, Value2), (Key1, Vaue3), (Key2, Value4), (Key2, Value5) ]
现在我想将其减少到这样的程度.
Now I want to reduce this, to something like this.
[ (Key1, [Value1, Value2, Value3]), (Key2, [Value4, Value5]) ]
也就是说,从键值到值的键列表.
That is, from Key-Value to Key-List of Values.
我该如何使用map并减少python或scala中的函数?
How can I do that using the map and reduce functions in python or scala?
推荐答案
collections.defaultdict
可以作为解决方案
查看全文