python中的MapReduce解决方案用于识别不对称对

时间:2013-05-23 03:54:25

标签: python python-2.7 mapreduce

我正在尝试用Python创建一个Mapper和Reducer,这是我不熟悉的语言,它将打印非对称键值对。例如: 输入

A,B
B,A
C,B
B,C
D,E
E,F
F,E
D,B

输出

D,E
D,B

2 个答案:

答案 0 :(得分:0)

>>> from collections import Counter
>>> pairs = [('A', 'B'), ('B', 'A'), ('C', 'B'), ('B', 'C'), ('D', 'E'), ('E', 'F'), ('F', 'E'), ('D', 'B')]
>>> [k for k, c in Counter(map(frozenset, pairs)).items() if c == 1]
[frozenset(['B', 'D']), frozenset(['E', 'D'])]

答案 1 :(得分:0)

向映射器添加两个发射。

def mapper(record):
    key = record[0]
    value = record[1]
    mr.emit_intermediate(key, value)
    mr.emit_intermediate(value, key)

def reducer(key, list_of_values):
    for n in list_of_values:
        if list_of_values.count(n) < 2:        
            output = key, n
            mr.emit((output))