Quick actions

cmd+k|ctrl+k

Navigation

Languages

word count

Snippet info

Language

Python

Visibility

public

Author

hltse59

Created

2024-04-15T12:55:33.031521Z

Updated

2024-04-15T13:26:09.273425Z

"""
Write a Python function that takes a string as input and returns the count 
of each word in the string.
"""
def word_count(x):
    words = x.split()           # copy string to list "words"
    # print(type(words))        # add words into list
    # print(words)              # show word list
    word_count_dict = {}        # create dictionary
    # print("word_count_dict : ", type(word_count_dict), "\n")
    for word in words:
        # print("current word is ", word)
        if word in word_count_dict:
            word_count_dict[word] += 1
        else:
            word_count_dict[word] = 1
        # print(word_count_dict)        # check point
    return word_count_dict

result = word_count("the quick brown fox jumps over the lazy dog")
print(result)
INFO