Created
May 8, 2012 13:36
-
-
Save suzuken/2635060 to your computer and use it in GitHub Desktop.
WordAverage by Header Word
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
# 標準入力を取得する | |
for line in sys.stdin: | |
line=line.strip() | |
words=line.split() | |
# 単語の頭文字とその文字数を返す | |
for word in words: | |
# 標準出力として | |
print '%s\t%s' % (word[0], len(word)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from operator import itemgetter | |
import sys | |
wordCount = {} | |
average = 0.0 | |
for line in sys.stdin: | |
line = line.strip() | |
word, count=line.split('\t', 1) | |
try: | |
count=int(count) | |
wordCount[word]['sum'] = wordCount.get(word['sum'], 0) + count | |
wordCount[word]['frequency'] = wordCount.get(word['frequency'], 0) + 1 | |
except ValueError: | |
pass | |
for word, values in wordCount: | |
average = values['sum'] / values['frequency'] | |
print '%s\t%s' % (word, average) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment