Class 22 — Wednesday March 6
It’s a fact, look it up
Really need to know — What lies farther down the road — Need a future map
Look both ways
Do you hear
- Tips to be a better listener.
Agenda
- Consider dict
- hacer 뭔가 frais oggi
Maxims (among others)
- Read the problem in its entirety before problem solving
- Determine for yourself why the sample problem solutions are as they are
- Problem solve before you code
- Check whether your solution produces the same answers as the sample runs
- Check whether your solution matches problem specifications
Huh?
- dónde está mi lápiz
- donde esta mi lapiz
- hvor 是 该 tumili maleri av Munch
- est la baguette fraîche aujourd'hui
- est la baguette fraiche aujourd'hui
Dictionary
- The human brain is great with associations; e.g., we typically associate apple with red and January 1st with New Year's Day.
- A
dict
is Python's software equivallent of a collection of associations.
- In particular, a
dict
is unnordered collection of associations.
- The elements of
dict
are called mappings. A mapping associates one object (called the key) with another object (called the value).
- The literal for an empty
dict
is{}
.
- With a dictionary and a key you can simply lookup its meaning.
- There is no simple way with a dictionary and a value to determine what key(s) map to the value.
- A literal mapping is written using the
:
to separate the key with its value. For example, the variableflora_to_color
defined below is adict
that associates flora with colors.
flora_to_color = { 'spruce': 'green', 'apple': 'red', 'shamrock': 'green',
'banana': 'yellow', 'potato': 'brown', 'eggplant': 'purple' }
- The
len()
function works on adict
.len(
flora_to_color
)
is 6, because the definition offlora_to_color
included six mappings.
- The brackets
[]
operator in conjunction with assignment can be used to put a mapping into adict
. The below statement adds the mapping'lemon'
to'yellow'
.
flora_to_color[ 'lemon' ] = 'yellow'
- The keys of a
dict
are always distinct. If there had already been a mapping with'lemon'
as the key, the above assignment would replace the association.
- As our
flora_to_color
example shows, the values of adict
are not necessarily unique.
- Other information about
dict
objects are listed below. In the below, assume thatd
is adict
.
d.get( k )
returns the value that keyk
maps to ind
. If there is no such value, returnsNone
.
d[ k ]
returns the value that keyk
maps to ind
. If there is no such value, an error occurs.
d[ k ] = v
setsd
to have a mapping fromk
tov
. If there was a prior mapping fromk
ind
, then that mapping is removed. In dictionary parlance, there is a now a mapping from keyk
to valuev
ind
.
del d[ k ]
removesk
’s mapping ind
.
( k in d )
indicates whetherk
is a key value ind
.
( k not in d )
indicates whetherk
is not a key value ind
.
d.clear()
removes all mappings ind
.
d.keys()
are the keys ford
. There are no duplicates among the keys.
d.values()
are the values of the keys ind
. There are duplicate values if more than one key maps to the same value.
d.pop( k )
removes k’s mapping in d and returns the value of that mapping.
Downloads
- Problem here_to_there.py
- Problem huh.py
Examples
- Problem good_doggie.py
- Problem what_did_you_say.py
To do
- Look over artifacts
- Information on a
dict
is always available in the module documentation section of the website.
- Additional information on collections is in general is available in the collections epistile.