From 0c0cea4351f35c7e20d1f455dece2129ea379c25 Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Fri, 18 Jan 2019 16:15:53 +0100 Subject: [PATCH] top-words solution We'll try rewriting that during next session --- sources/06-top-words.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/sources/06-top-words.py b/sources/06-top-words.py index d89f4a2..8654a0b 100644 --- a/sources/06-top-words.py +++ b/sources/06-top-words.py @@ -7,10 +7,10 @@ def get_value(pair): def get_word(chunk): - if all(x.isalpha() for x in chunk): + if all(x.isalpha() for x in chunk): # is_alpha() if len(chunk) < 4: return None - return chunk.lower() + return chunk.lower() # lower() else: return None @@ -18,19 +18,28 @@ def get_word(chunk): def main(): filename = sys.argv[1] file = open(filename, "r") + lines = file.readlines() + file.close() scores = {} - for line in file.readlines(): + for line in lines: for chunk in line.split(): word = get_word(chunk) if word: if not word in scores: - scores[word] = 0 + scores[word] = 1 else: scores[word] += 1 - file.close() + to_sort = [] + for k in scores: # iterate on dicts + v = scores[k] + to_sort.append([v, k]) + to_sort.sort() + + + print(to_sort[-10:]) main()