EDIT: Don't read my post, if you are only allowed to use the primitives.
You can use punctuation from strings module.
Then you can iterate over the string and look if the
char is not in the punctuation string and add it to
the result. The result is a list in the first and second example.
To convert a list with strings into one string, you can
use the
The teacher want that you understand the algorithm.
We want, that you understand Python.
When you have some kind of a collection,
you can count the occurrence of unique elements with Counter from collections module.
which is in this case
If you access a key, which does not exist it returns a 0.
With the first occurrence of a word, which is not as a key in the dict, 0 is returned.
This is the cause, why the inline addition works.
You can use punctuation from strings module.
Then you can iterate over the string and look if the
char is not in the punctuation string and add it to
the result. The result is a list in the first and second example.
To convert a list with strings into one string, you can
use the
str.join('', result)
or''.join(result)
.from string import punctuation def punctuation_filter1(text): """ Naive implementation with a for loop and a list """ result = [] for char in text: if char not in punctuation: result.append(char) return ''.join(result) def punctuation_filter2(text): """ List Comprehension """ result = [char for char in text if char not in punctuation] return ''.join(result) def punctuation_filter3(text): """ A generator expression inside a function call """ return ''.join(char for char in text if char not in punctuation) print('Punctuation:', punctuation) testtext = '!?.,Hello World!' print('testtext:', testtext) print('punctuation_filter1:', punctuation_filter1(testtext)) print('punctuation_filter2:', punctuation_filter1(testtext)) print('punctuation_filter3:', punctuation_filter1(testtext))Now back to your original problem.
The teacher want that you understand the algorithm.
We want, that you understand Python.
When you have some kind of a collection,
you can count the occurrence of unique elements with Counter from collections module.
from collections import Counter words = "hello my friend, are you my friend? I need some help, hello? hello?" # first filtering punctuation, then lower case, then split clean_word_list = punctuation_filter3(words).lower().split() result = Counter(clean_word_list) print(result.most_common())Another older solution is with defaultdict.
from collections import defaultdict counter = defaultdict(int) for word in clean_word_list: counter[word] += 1 print(counter)A
defaultdict
has a default factory for missing keys,which is in this case
int
. Calling int()
returns 0
.If you access a key, which does not exist it returns a 0.
With the first occurrence of a word, which is not as a key in the dict, 0 is returned.
This is the cause, why the inline addition works.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!