Skip to content

dict.get() example needs clarification #31

Description

@Akuli
<Arrinao> sentence = input("Enter a sentence: ")
<Arrinao> counts = {}
<Arrinao> for word in sentence.split():
<Arrinao>     counts[word] = counts.get(word, 0) + 1
<Akuli> two cases: word is not in counts, word is in counts
<Akuli> word not in counts:   counts[word] = 0+1
<Akuli> s o when we see it for the first time, set count to 1
<Akuli> and when word is in counts, it does:   counts[word] = counts[word] + 1
<Akuli> so when we see a word we already saw before, we increment the count

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions