40c2fc5f55
This commit was SVN r26379.
39 строки
797 B
Python
Исполняемый файл
39 строки
797 B
Python
Исполняемый файл
#!/usr/bin/env python
|
|
|
|
from operator import itemgetter
|
|
import sys
|
|
|
|
words = []
|
|
cnt = []
|
|
|
|
# input comes from STDIN
|
|
for line in sys.stdin:
|
|
# remove leading and trailing whitespace
|
|
line = line.strip()
|
|
|
|
# parse the input we got from mapper.py
|
|
word, count = line.split('\t', 1)
|
|
|
|
# convert count (currently a string) to int
|
|
try:
|
|
count = int(count)
|
|
except ValueError:
|
|
# count was not a number, so silently
|
|
# ignore/discard this line
|
|
continue
|
|
|
|
# Add the word to the list count times
|
|
try:
|
|
n = words.index(word)
|
|
cnt[n] += count
|
|
except ValueError:
|
|
words.append(word)
|
|
cnt.append(count)
|
|
|
|
# output the final count
|
|
print 'Final word count:'
|
|
n=0
|
|
for wrd in words:
|
|
print '%s\t%d' % (wrd, cnt[n])
|
|
n += 1
|