SECTION 28 / 31

Index noun and pronoun positions with lists

1 min read
partOfSpeech has Noun and Pronoun keys whose VALUES are ordered lists of zero-based word positions.
partOfSpeech has Noun and Pronoun keys whose VALUES are ordered lists of zero-based word positions.
REAL COURSE DATA · KEYS, COUNTS AND CONNECTIONS

Collect noun and pronoun positions

Follow the dictionary as its keys and values change. Predict each lookup or update before revealing the next step.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next key, whether it already exists, and the value that will be stored.

partOfSpeech = {}
partOfSpeech["Noun"] = []
partOfSpeech["Pronoun"] = []
# Process word records in ascending SerialNumber.
foreach X in words {
    if (X.PartOfSpeech == "Noun") {
        partOfSpeech["Noun"] = partOfSpeech["Noun"] ++ [X.SerialNumber]
    }
    if (X.PartOfSpeech == "Pronoun") {
        partOfSpeech["Pronoun"] = partOfSpeech["Pronoun"] ++ [X.SerialNumber]
    }
}

The dataset field is SerialNumber, beginning at 0. The slides use SerialNo for the same position role. Our lab maps the concept to the actual field. It uses the supplied part-of-speech labels without retagging the text. There are 21 Noun records and 7 Pronoun records.

The noun positions are sorted because the INPUT is processed in ascending position and positions are appended. A dictionary does not sort the contents of a list value for you. If word cards arrive shuffled, restore position order before relying on the early-exit matcher.

Try it yourself

What does partOfSpeech[Pronoun] contain?

Show explained answer

A list of word positions, not a list of pronoun spellings and not a dictionary count.

Try it yourself

Does the dictionary’s key order determine the list’s order?

Show explained answer

No. The input scan and append order determine the list order.

Try it yourself

Why create both keys with [] first?

Show explained answer

Appending is safe even before the first noun or pronoun is encountered. A key can exist with an empty list value.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑