-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_counter.py
33 lines (29 loc) · 1.42 KB
/
Word_counter.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
def word_counter(text):
"""
This function first splits the 'text', if the two words contain space between them it creates a
list consisting of the two words and a blanK value as a compensation to the space deleted and then
it it deletes the blanK string.
Analysis of lines of code:-
(1)Spliting the 'text' into words and blanK strings.
(2)Creating a new variable which stores a list which is filtered version of the list generated by
spliting the 'text'.
(3)Returns a message that tells how many words are in the text
What is a blanK string?
Suppose the text is 'Hello World'. When this is splited, the list so formed is ['Hello','','World']
Now the second item in the list is a blanK string
"""
words = text.split(' ')
pure_words = list(filter(lambda x: x != "", words))
return f'''In the text file, there are {len(pure_words)} words'''
def list_of_words_from_text(text):
"""
So, given a text, this function sorts out blanK spaces and returns a list of words without any blanK
spaces and null strings.
for example:-
text = "Hello World, this program is believed to give good lucK.
When this function is used it will return the beow displayed list:-
list_returned = ['Hello','World','this','program','is'.'believed','to','give','good','lucK.']
"""
words = text.split(" ")
valid_words = list(filter(lambda x: x != "", words))
return valid_words