Search code examples
pythonfrequency

Count word frequency without using count()


Python counts every character, but not word. Something to do to change? Not using count()

dict = {}
def convert(sentence):
    return (sentence.split())
 

sentece =  input("write something: ")
print( convert(sentence))


for item in sentence:
    if item not in ordbok:
        dict[item] = 0
    dict[item] += 1
print(dict)

Solution

  • Please find below the issues with your current logic:

    • iterate on the return list you get after convert(sentence).

    • if you iterate on the sentence, it will take character count

    Please find the code below with the corrections:

    dict = {}
    
    def convert(sentence):
        return (sentence.split())
    
    sentence = input("write something: ")
    print(convert(sentence))
    
    for item in convert(sentence):
        if item not in dict:
            dict[item] = 0
        dict[item] += 1
    print(dict)