-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
45 lines (31 loc) · 1.07 KB
/
stats.py
File metadata and controls
45 lines (31 loc) · 1.07 KB
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
def word_count(book_text):
count = 0
word_list = book_text.split()
for word in word_list:
count += 1
return count
def character_count(text_string):
counts = {}
for letter in text_string:
if letter.lower() in counts:
counts[letter.lower()] += 1
else:
counts[letter.lower()] = 1
return counts
def sort_on(dict_item):
# Get the first (and only) key in the dictionary
key = list(dict_item.keys())[0]
# Return the value associated with that key
return dict_item[key]
def dict_sort(dictionary):
sorted_list = [] # this is a list of dictionaries
# This works, but isn't very Python-like
# for entry in dictionary:
# if entry.isalpha() is True:
# sorted_list.append({entry:dictionary[entry]})
# This way of iterating through key-value pairs is the more Python way
for key, value in dictionary.items():
if key.isalpha() is True:
sorted_list.append({key: value})
sorted_list.sort(reverse=True, key=sort_on)
return sorted_list