I wrote a code that given a string (txt), will print a histogram that contains the number of occurrences of each word in it and the word itself.
A word is defined to be a sequence of chars separated by one space or more.
The code is working, I just want to see if I can improve it.
def PrintWordsOccurence(txt):
lst = [x for x in txt.split(' ') if x != ' ' and x != '']
newlst = list(dict.fromkeys(lst))
for item in newlst:
print("[{0}] {1}".format(lst.count(item),item))
An input-output example:
>> PrintWordsOccurence("hello code review! this is my code")
[1] hello
[2] code
[1] review!
[1] this
[1] is
[1] my
The first line creates a list with all words, there will be duplicates in it, so to remove them, I turn the list into a dictionary and then I turn it back to a list. In the end, for each item in the list, I print the number of its occurrences in the original string and the item itself.
The time complexity of the code is \$\Theta(n^2)\$
Any suggestions to improve the code?