Alterna · учебный материал

dictionaries

40.2 dictionaries: Short theory, one example, and Python step-by-step.

Что в этом материале

Содержание и основные понятия

The dictionary stores pairs of keys of value. A frequent dictionary turns each object into a key and the number of appearances into a value.
substance and practice
Grade 11
EEG No. 24
01
How it works.
The dictionary connects a unique key to a value. A frequent dictionary stores an object as a key, and the number of its appearances as a meaning.
The key.
Heshable unique value, e.g. string or number.
Meaning.
Key-related data; may be of any type.
Frequency.
The ket(key, 0) allows the counter to start from scratch.
No one.
If multiple keys are equal in frequency, the choice rule needs to be made clearly.
02
One example in execution
Start the short program and go over the lines. The visualiser only shows the code, current line, variables and output.
Treatment
data[key]
requires an existing key and otherwise gives
KeyError
.
get
Returns the emergency value without changing the dictionary. Use it to bypass steam.
items()
;
keys()
and
values()
Don't change the size of the dictionary while you're bypassing it.
items()
.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Take the key
3
Get the frequency get(key, 0)
4
Increase value
5
Use dictionary
03
Remember
Set the rule to no one before calling max/min.
Do not use d[key] for the first increase in frequency without get or in check.
The keys must be hashable; the list cannot be the key.
Syntax of this page
{} / dict()
They create an empty dictionary.
d[key]
Reads or records the value by key; reading the missing key gives KeyError.
d.get(key, default)
Returns the value or default without adding the key.
d.items()
Selects pairs (key, value).
d.keys() / d.values()
Returns the representation of keys or values.
d.pop(key, default)
Removes the key and returns the value; default prevents KeyError.
setdefault(key, default)
Returns the key value by adding default in absence.
zip(a, b)
Collects pairs of elements of two sequences.
min(iterable, key=...)
Selects the element with the minimum key; lambda is here asking tie-break.
lambda ch: ...
A short function for a key to a choice.
strip / input / print
Read the text, normalize the edges and produce the result.

Structural map of textbooks