Skip to content

Instantly share code, notes, and snippets.

@suzuken
Created May 8, 2012 13:36
Show Gist options
  • Save suzuken/2635060 to your computer and use it in GitHub Desktop.
Save suzuken/2635060 to your computer and use it in GitHub Desktop.
WordAverage by Header Word
#!/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))
#!/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