python:使用if条件语句按键减少? [英] python : reduce by key with if condition statement?

查看:76
本文介绍了python:使用if条件语句按键减少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(K1, (v1, v2))
(K2, (v3, v4))
(K1, (v1, v5))
(K2, (v3, v6))

如果第一个值是 some 或 eque 使得我得到 (k1, (v1,v2+v5), (k2,(v3,v4+v6) ?

How can I sum up the values of the key provided the first value is the some or eque such that I get (k1, (v1,v2+v5), (k2,(v3,v4+v6) ?

推荐答案

IIUC,您需要更改 reduce 之前的键,然后将您的值映射回所需的格式.

IIUC, you need to change the key before the reduce, and then map your values back in the desired format.

您应该能够执行以下操作:

You should be able to do the following:

new_rdd = rdd.map(lambda row: ((row[0], row[1][0]), row[1][1]))\
    .reduceByKey(sum).
    .map(lambda row: (row[0][0], (row[0][1], row[1])))

这篇关于python:使用if条件语句按键减少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆