0

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

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

输出

D,E
D,B
4

2 回答 2

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'])]
于 2013-05-23T03:59:15.230 回答
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))
于 2013-05-26T10:44:52.430 回答