Ch 11 - Dictionaries Flashcards
Think Julia
What is a dictionary?
**A Dictionary Is a Mapping **
A dictionary is like an array, but more general. In an array, the indices have to be integers; in a dictionary they can be (almost) any type.
A dictionary contains a collection of indices, which are called keys, and a collection of values. Each key is associated with a single value. The association of a key and a value is called a key-value pair, or sometimes an item.
In mathematical language, a dictionary represents a mapping from keys to values, so you can also say that each key “maps to” a value. As an example, we’ll build a dictionary that maps from English to Spanish words, so the keys and the values are all strings.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4140-4149). Kindle Edition.
Think Julia
How can you create a new empty dictionary?
The function Dict creates a new dictionary with no items (because Dict is the name of a built-in function, you should avoid using it as a variable name):
julia > eng2sp = Dict()
Dict{ Any, Any} with 0 entries
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4149-4154). Kindle Edition.
Think Julia
How can you add an item to a dictionary?
julia > eng2sp = Dict()
Dict{ Any, Any} with 0 entries
The types of the keys and values in the dictionary are specified in curly braces: here, both are of type Any. The dictionary is empty. To add items to the dictionary, you can use square brackets:
julia > eng2sp[” one”] = “uno”;
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4152-4165). Kindle Edition.
Think Julia
How can you create a new dictionary with items in it?
you can create a new dictionary with three items as follows:
julia > eng2sp = Dict(“ one” = > “uno”, “two” = > “dos”, “three” = > “tres”)
Dict{ String, String} with 3 entries:
“two” = > “dos”
“one” = > “uno”
“three” = > “tres”
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4172-4178). Kindle Edition.
Think Julia
How are dictionaries ordered?
The order of the items in a dictionary is unpredictable. If you type the same example on your computer, you might get a different result.
But that’s not a problem because the elements of a dictionary are never indexed with integer indices. Instead, you use the keys to look up the corresponding values:
julia > eng2sp[” two”]
“dos”
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4180-4187). Kindle Edition.
Think Julia
How can you check if something is a key in a dictionary?
Now you can use the ∈ operator to see whether something appears as a key in the dictionary:
julia > “one” ∈ ks ………# ks is a dictionary
true
julia > “uno” ∈ ks
alse
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4205-4210). Kindle Edition.
Think Julia
How can you find the number of keys in a dictionary?
The length function works on dictionaries; it returns the number of key-value pairs:
julia > length( eng2sp)
3
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4194-4198). Kindle Edition.
Think Julia
How can you return an array of all the keys in a dictionary.
The function keys returns a collection with the keys of the dictionary:
julia > ks = keys( eng2sp);
julia > print( ks)
[” two”, “one”, “three”]
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4198-4204). Kindle Edition.
Think Julia
How can you check if something is a value in a dictionary?
To see whether something appears as a value in a dictionary, you can use the function values, which returns a collection of values, and then use the ∈ operator:
julia > vs = values( eng2sp);
julia > “uno” ∈ vs
true
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4211-4218). Kindle Edition.
Think Julia
How can you have a function throw an exception?
function reverselookup( d, v)
….for k in keys( d)
……..if d[ k] = = v
…………return k
……..end
….end
….error(“ LookupError”)
end
This function is yet another example of the search pattern, but it uses a function we haven’t seen before: error. The error function is used to produce an ErrorException that interrupts the normal flow of control. In this case it has the message “LookupError”, indicating that a key does not exist.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4313-4328). Kindle Edition.
Think Julia
How can dictionaries and arrays cooperate with each other?
Dictionaries and Arrays
Arrays can appear as values in a dictionary. For example, if you are given a dictionary that maps from letters to frequencies, you might want to invert it— that is, create a dictionary that maps from frequencies to letters. Since there might be several letters with the same frequency, each value in the inverted dictionary should be an array of letters.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4343-4349). Kindle Edition.
Think Julia
What quality must a dictionary key have?
I mentioned earlier that a dictionary is implemented using a hash table. That means that the keys have to be hashable.
A hash is a function that takes a value (of any kind) and returns an integer. Dictionaries use these integers, called hash values, to store and look up key-value pairs.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4378-4383). Kindle Edition.
Think Julia
What is a: hash
I mentioned earlier that a dictionary is implemented using a hash table. That means that the keys have to be hashable.
A hash is a function that takes a value (of any kind) and returns an integer. Dictionaries use these integers, called hash values, to store and look up key-value pairs.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4378-4383). Kindle Edition.
Think Julia
What is a: memo
One solution is to keep track of values that have already been computed by storing them in a dictionary. A previously computed value that is stored for later use is called a memo. Here is a “memoized” version of fibonacci:
known = Dict( 0 = > 0, 1 = > 1)
function fibonacci( n)
….if n ∈ keys( known)
……..return known[ n]
….end
….res = fibonacci( n-1) + fibonacci( n-2)
….known[ n] = res
….res
end
known is a dictionary that keeps track of the Fibonacci numbers we already know. It starts with two items: 0 maps to 0 and 1 maps to 1.
Whenever fibonacci is called, it checks known. If the result is already there, it can return immediately. Otherwise, it has to compute the new value, add it to the dictionary, and return it.
If you run this version of fibonacci and compare it with the original, you will find that it is much faster.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4414-4418). Kindle Edition.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4397-4413). Kindle Edition.
Think Julia
What is a: main
Global Variables
In the previous example, known is created outside the function, so it belongs to the special frame called Main. Variables in Main are sometimes called global because they can be accessed from any function. Unlike local variables, which disappear when their function ends, global variables persist from one function call to the next.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 4419-4424). Kindle Edition.