forked from fenyx-it-academy/Class7-Python-Module-Week2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2.py
More file actions
21 lines (16 loc) · 888 Bytes
/
Q2.py
File metadata and controls
21 lines (16 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Write a code snippet that inputs a sentence from the user, then uses a dictionary to summarize the number of occurrences of each letter. Ignore case, ignore blanks and assume the user does not enter any punctuation. Display a two-column table of the letters and their counts with the letters in sorted order.
# ```
# Example
# Input >>> "This is a sample text with several words This is more sample text with some different words"
# Output >>>
# [('a', 4), ('d', 3), ('e', 10), ('f', 2), ('h', 4), ('i', 7), ('l', 3), ('m', 4), ('n', 1), ('o', 4), ('p', 2), ('r', 5), ('s', 10), ('t', 9), ('v', 1), ('w', 4), ('x', 2)]
# ```
example = 'This an example sentence'*3
def extract_characters(example):
examplelist = list(example)
char = {}
for cha in set(examplelist):
char[cha] = examplelist.count(cha)
return char
char = extract_characters(example)
print(char)