mercredi 23 janvier 2019

How can it search inside the directory, if the directory is empty?

I just started learning python and found this snippet. It's supposed to count how many times a word appears. I guess, for all of you this will seem very logical, but unfortunately for me, it doesn't make any sense.

str = "house is where you live, you don't leave the house."
dict = {}
list = str.split(" ") 
for word in list:  # Loop over the list
    if word in dict:  # How can I loop over the dictionary if it's empty?
        dict[word] = dict[word] + 1
    else:
        dict[word] = 1

So, my question here is, how can I loop over the dictionary? Shouldn't the dictionary be empty because I didn't pass anything inside? Maybe I am not smart enough, but I don't see the logic. Can anybody explain me how does it work? Many thanks

Aucun commentaire:

Enregistrer un commentaire