-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (48 loc) · 1.93 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def main():
# Open and read the file
with open("books/frankenstein.txt") as f:
file_contents = f.read()
# Create empty dictionary
letter_count = {}
# Convert to lowercase
file_contents = file_contents.lower()
# Loop through each letter
for l in file_contents:
if l.isalpha():
# If letter exists in dictionary, add 1 to its count
if l in letter_count:
letter_count[l] += 1
# If it doesn't exist, set its count to 1
else:
letter_count[l] = 1
# print "Frankenstein" text
print(file_contents)
# print empty line for readability
print()
# print the number of words
wl = file_contents.split()
print(len(wl))
# print empty line for readability
print()
# print the dictionary
print(letter_count)
# print empty line for readability
print()
print("--- Begin a report of books/frankenstein.txt ---")
print(f"{len(wl)} words found in the document.")
print()
# Function to extract the value part of a tuple
def get_value(item):
return item[1]
# create a variable, sorted_items, .items() creates a list of tuples
# from the dictionary. sorted() sorts the dictionary using your function
# get_value by passing the key=get_value() which means sort by using the
# number in the dictionary's key-value pairs. reverse=True tells it to
# do so in descending order.
sorted_items = sorted(letter_count.items(), key=get_value, reverse=True)
# now a for loop to iterate over your list of tuples
for item in sorted_items:
letter = item[0] # First element: the letter
frequency = item[1] # Second element: the frequency
print(f"The '{letter}' character was found {frequency} times")
main()