Hello everyone for my problem i have to input a text and a few words.
The result I need to get is that the program shows how many time each word occurs in the text. An example of the expected input and output will be shown at the bottom of this file
The code I have at the moment is this
tekst = str(input().lower())
wordsToCount = str(input().lower())
D = dict()
words = tekst.split()
wordsToCount = wordsToCount.split()
for word in words:
for wordToCount in wordsToCount:
if wordToCount == word:
if wordToCount not in D:
D[wordToCount] = 1
else:
D[wordToCount] = 1
for key in list(D.keys()):
print(D[key])
With output
3 1 2 1
This seems really close but It takes the word "Or" first instead of "reading" because "or" comes first in the text
INPUT:
Alice was beginning to get very tired of sitting by her sisiter on the
bank and of having nothing to do once or twice she had peeped into the book her sister was reading but it had no pictures or conversations in it and what is the use of a book thougt Alice
without pictures or conversations
-----
reading
or
what
pictures
Output:
1
3
1
2
CodePudding user response:
You are relying on the default dictionary key ordering If you need the results to be printed in exactly a certain order, then you need do that yourself.
for word in wordsToCount:
print(D[word])
CodePudding user response:
The collections
module has a Counter
class that is exactly what you need for this. It's a dict
subclass whose initialization takes an iterable containing items to be counted. The resulting dictionary has the count of each word. You can use a generator expression to filter the words being counted.
from collections import Counter
# same input and initialization of your variables
D = Counter(w for w in words if w in wordsToCount)
You can also make wordsToCount
a set
to make things a little faster.
The order is likely because you're using a Python version before 3.6. 3.6 and later retain the order of keys as they are added. If you need your code to run on Python prior to 3.6, get the keys from wordsToCount
instead of from the dict (and keep it a list so its order is retained). Since some wordsToCount may not appear in the original tekst, check to see if each word is in the dictionary before printing.
for key in wordsToCount:
if key in D:
print(D[key])