首页 > 解决方案 > 如果我需要遍历所有映射器值,我应该如何创建 reduce?

问题描述

我的映射器有两个收益:

yield (destination,{'origin':origin,'count':count})
yield (origin,{'destination':destination,'count':count})

这给了我如下输出:

('United States', {'origin': 'Romania', 'count': '1'})
('Romania', {'count': '1', 'destination': 'United States'})
('United States', {'origin': 'Ireland', 'count': '264'})
('Ireland', {'count': '264', 'destination': 'United States'})
('United States', {'origin': 'India', 'count': '69'})
('India', {'count': '69', 'destination': 'United States'})
('Egypt', {'origin': 'United States', 'count': '24'})
('United States', {'count': '24', 'destination': 'Egypt'})
('Equatorial Guinea', {'origin': 'United States', 'count': '1'})
('United States', {'count': '1', 'destination': 'Equatorial Guinea'})
('United States', {'origin': 'Singapore', 'count': '25'})
('Singapore', {'count': '25', 'destination': 'United States'})

现在我需要编写一个reducer,我可以将上述数据分成两组并使用两个for循环进行迭代。我们的想法是找出所有可能的两次跳跃的总和

例如:

从罗马尼亚到埃及。我们采取Romania -> United States and United States -> Egypt. The total possible flights would then be 1*24

据我了解,我需要遍历所有值两次,并找到键Destination of 1 == origin of another.

到目前为止,我认为我需要创建两个组:g1(对于所有基于源的键)和 g2(对于所有基于目标的键)

然后遍历每个组找到这样g1.destination == g2.origin的 .

我是否朝着正确的方向前进?如果是这样,这样的函数在 reducer 中的外观如何?老实说,我似乎无法弄清楚我应该如何编码。

标签: pythonmapreduce

解决方案


推荐阅读