Skip to content

Instantly share code, notes, and snippets.

@masayang
Created March 10, 2013 22:50
Show Gist options
  • Save masayang/5130862 to your computer and use it in GitHub Desktop.
Save masayang/5130862 to your computer and use it in GitHub Desktop.
共通友達を数えるMrJob
W, A, B, C
X, A, B, C, D, Z
Y, A, B, E
Z, A, B, D, E
from mrjob.job import MRJob
class Recommendation(MRJob):
def mapper(self, key, line):
input = line.split(',')
user, friends = input[0], input[1:]
for i in range(len(friends)):
f1 = friends[i].strip()
for j in range(i+1, len(friends)):
f2 = friends[j].strip()
if f1 < f2:
yield(f1, f2), 1
else:
yield(f2, f1), 1
def reducer(self, key, values):
f1, f2 = key
mutual_friends_count = 0
for value in values:
mutual_friends_count += value
yield (f1, f2), mutual_friends_count
if __name__ == '__main__':
Recommendation.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment