[Python] Python을 이용한 MapReduce

최지영·2022년 10월 10일
0

📜 Python을 이용한 MapReduce 구현

def mapreduce(data: str):

    current_word = None
    current_count = 0
    word = None
    splited_word = data.split()

    word_li = []
    for s in splited_word:
        word_li.append((s, 1))

    sorted_list = sorted(word_li, key=lambda m: m[0])

    for li in sorted_list:
        word = li.__getitem__(0)
        count = li.__getitem__(1)
        if current_word == word:
            current_count += count
        else:
            if current_word:
                print('%s\t%s' % (current_word,current_count))
            current_count = count
            current_word = word
    if current_word == word:
        print('%s\t%s' % (current_word, current_count))

mapreduce("poo bar poo bar hello world world hello word poo barbar")

✨ 결과


bar	2
barbar	1
hello	2
poo	3
word	1
world	2

0개의 댓글