-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_words_from_file.py
35 lines (28 loc) · 1.43 KB
/
group_words_from_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
""" Use a Python dictionary to group the words in a text file according to
their length (number of letters)
"""
import sys # For argv global command line arguments list
def main():
""" Group the words by length in a text file """
if len(sys.argv) < 2: # Did the user not supply a file name?
print('Usage: python3 group_words_from_file.py <file_name>')
print('\tWhere <file_name> is the name of the text file.')
else: # User provided file name
file_name = sys.argv[1]
word_groups = {} # Init grouping dict
with open(file_name, 'r') as file_obj: # Open file for reading
file_content = file_obj.read() # Read in content of the entire file
words = [word.upper() for word in file_content.split()] # Make list of individual words (All-caps)
for word in words:
word_len = len(word)
if word_len in word_groups:
if word not in word_groups[word_len]: # Avoid duplication
word_groups[word_len] += [word] # Add th word to its group
else:
word_groups[word_len] = [word] # Add the word to a new group
# Display
for word_len, word_group in word_groups.items():
print('{0} : {1}'.format(word_len, word_group))
if __name__ == '__main__':
main()