Python from scratch- The journey continues
Python from scratch- The journey continues
Join the DZone community and get the full member experience.
Join For FreeJumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.
Three weeks ago I published my first post about how I started my journey into
Python.
# Define print_words(filename) and print_top(filename) functions.
# You could write a helper utility function that reads a file
# and builds and returns a word/count dict for it.
# Then print_words() and print_top() can just call the utility function.
1. For the --count flag, implement a print_words(filename) function that counts how often each word appears in the text and prints:
word1 count1
word2 count2
...
Print the list in order sorted by word (python will sort punctuation to come before letters- that's fine).
Store all the words as lowercase, so 'The' and 'the' count as the same word.
2. For the --topcount flag implement a print_top(filename) which is similar to print_words() but which prints just the top 20 most common words sorted so the most common word is first, then the next most common and so on
import sys def calculate_data (line_data): # Got a sring and creates dictionary of (word : count) dict = {} for word in line_data: if word in dict: dict[word] += 1 else: dict[word] = 1 return dict def file_to_dict(file1): # convert file into string by lower case and removing \n data = file1.read() data = data.replace ('\n', ' ') data = data.lower() # Split by space for word in data: line_data = data.split (" "); # Sending to function that creates and return dictionary return calculate_data (line_data) def print_top (filename): dict1 = {} file1 = open (filename,'rU') dict1 = file_to_dict (file1) # convert file into dictionary # Print the dictionary top20 sorted by values- The solution. dict1 = sorted(dict1.items(), key=lambda (k,v):(v,k), reverse=True) for i in range (5): print dict1[i] file1.close () return filename def print_words (filename): dict1 = {} file1 = open (filename,'rU') dict1 = file_to_dict (file1) # convert file into dictionary # Print the dictionary sorted- The solution. print sorted (dict1.items()) file1.close () return filename def main(): if len(sys.argv) != 3: print 'usage: ./wordcount.py {--count | --topcount} file' sys.exit(1) option = sys.argv[1] filename = sys.argv[2] if option == '--count': print_words(filename) elif option == '--topcount': print_top(filename) else: print 'unknown option: ' + option sys.exit(1) if __name__ == '__main__': main()
Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.
Published at DZone with permission of Hod Benbinyamin . See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}