[Tutor] collections and mappings
Steven D'Aprano
steve at pearwood.info
Fri Jun 21 21:57:07 EDT 2019
On Fri, Jun 21, 2019 at 10:01:43AM +1000, mhysnm1964 at gmail.com wrote:
> I have reviewed the collection module and do not understand mappings. I have
> seen this in other languages and have never got the concept. Can someone
> explain this at a very high level.
It might help if you remember that dicts are a kind of mapping.
Another name for dicts are "associative arrays".
In the most general terms, a mapping is an association between one or
more pieces of information and another one or more pieces of
information. That is so general as to be completely abstract, and I
think it will be more useful to give some concrete examples.
If you are American, you probably have a social security number. There
is a mapping between your social security number and you, the person:
the government associates data about you with the social security
number.
If you have used a regular paper dictionary, it is a mapping between
words and definitions. We say the word maps to the definition.
In Python terms, we would use a dict, using the word as the key and the
definition as the value. For example:
{"Childhood":
""""The period of human life intermediate between the idiocy
of infancy and the folly of youth—two removes from the sin of
manhood and three from the remorse of age.""",
"Debt":
"""An ingenious substitute for the chain and whip of the
slave-driver.""",
"Piano":
"""A parlor utensil for subduing the impenitent visitor. It
is operated by depressing the keys of the machine and the
spirits of the audience.""",
"Quotation":
"""The act of repeating erroneously the words of another.
The words erroneously repeated."""
}
Does this help?
--
Steven
More information about the Tutor
mailing list