6. Dictionaries#

A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type.

You can think of a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps to a value. The association of a key and a value is called a key-value pair or sometimes an item.

As an example, we’ll build a dictionary that maps from English to Spanish words, so the keys and the values are all strings.

A empty dictionary can be created using the dict function

D = dict()

or just by using {}

D = {}

key value pairs can be added to dictionary using [].

D["esdger"] = "dijksta"
D["ada"] = "lovelace"

A value can be found in a dictionary using the key

D["ada"]
'lovelace'

and the value can be changed.

D["ada"] = "Byron"
print(D)
{'esdger': 'dijksta', 'ada': 'Byron'}

6.1. Exercise 1#

Do dictionary keys have to be of the same type ?

6.2. Solution 1#

D[3.1] = "pi"

6.3. Exercise 2#

Do dictionary values have to be of the same type ?

6.4. Solution 2#

D[2.1] = 7.2

6.5. Exercise 3#

Do dictionary values have to be unique ?

D[2.2] = 7.1

6.6. Exercise 4#

What types can be used as keys in a dictionary ? Experiment and find out.

Dictionaries can be created with content using {:}

DD = {1 : 3,"a" : (1,True)}
print(DD)
{1: 3, 'a': (1, True)}

The in operator can be used to find if a value is in sequence, be it a list, tuple, or dictionary.

L = [1,2,3]
2 in L
True
T = (1,"hello",True)
False in T
False

In a dictionary, in operates on the keys, not the values.

D = {"a" : 1,(2,3) : True}
(2,3) in D
True

The in operator uses different algorithms for lists and dictionaries. For lists, it uses a search algorithm. As the list gets longer, the search time gets longer in direct proportion to the size of the list. For dictionaries, Python uses an algorithm called a hashtable that has a remarkable property: the in operator takes about the same amount of time no matter how many items there are in a dictionary.

6.7. Exercise 5#

Install the numpy library using pip.

6.8. Solution 5#

!python -m pip install numpy
Requirement already satisfied: numpy in /home/grosedj/work/work-env/env/lib/python3.9/site-packages (1.23.2)
WARNING: You are using pip version 21.2.4; however, version 23.3.2 is available.
You should consider upgrading via the '/home/grosedj/work/work-env/env/bin/python -m pip install --upgrade pip' command.

6.9. Exercise 6#

import the numpy library. Use help to find out about the poisson function in the random module of numpy. Note - the numpy library is discussed in more detail in a seperate chapter.

6.10. Solition 6#

import numpy
help("numpy.random.poisson")
Help on built-in function poisson in numpy.random:

numpy.random.poisson = poisson(...) method of numpy.random.mtrand.RandomState instance
    poisson(lam=1.0, size=None)
    
    Draw samples from a Poisson distribution.
    
    The Poisson distribution is the limit of the binomial distribution
    for large N.
    
    .. note::
        New code should use the ``poisson`` method of a ``default_rng()``
        instance instead; please see the :ref:`random-quick-start`.
    
    Parameters
    ----------
    lam : float or array_like of floats
        Expected number of events occurring in a fixed-time interval,
        must be >= 0. A sequence must be broadcastable over the requested
        size.
    size : int or tuple of ints, optional
        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
        a single value is returned if ``lam`` is a scalar. Otherwise,
        ``np.array(lam).size`` samples are drawn.
    
    Returns
    -------
    out : ndarray or scalar
        Drawn samples from the parameterized Poisson distribution.
    
    See Also
    --------
    random.Generator.poisson: which should be used for new code.
    
    Notes
    -----
    The Poisson distribution
    
    .. math:: f(k; \lambda)=\frac{\lambda^k e^{-\lambda}}{k!}
    
    For events with an expected separation :math:`\lambda` the Poisson
    distribution :math:`f(k; \lambda)` describes the probability of
    :math:`k` events occurring within the observed
    interval :math:`\lambda`.
    
    Because the output is limited to the range of the C int64 type, a
    ValueError is raised when `lam` is within 10 sigma of the maximum
    representable value.
    
    References
    ----------
    .. [1] Weisstein, Eric W. "Poisson Distribution."
           From MathWorld--A Wolfram Web Resource.
           http://mathworld.wolfram.com/PoissonDistribution.html
    .. [2] Wikipedia, "Poisson distribution",
           https://en.wikipedia.org/wiki/Poisson_distribution
    
    Examples
    --------
    Draw samples from the distribution:
    
    >>> import numpy as np
    >>> s = np.random.poisson(5, 10000)
    
    Display histogram of the sample:
    
    >>> import matplotlib.pyplot as plt
    >>> count, bins, ignored = plt.hist(s, 14, density=True)
    >>> plt.show()
    
    Draw each 100 values for lambda 100 and 500:
    
    >>> s = np.random.poisson(lam=(100., 500.), size=(100, 2))

6.11. Exercise 7#

Generate a sample of size 100 from a poisson distribution with \(\lambda = 2\).

6.12. Solution 7#

S = numpy.random.poisson(2,100)
print(S)
[1 2 0 2 1 2 3 3 1 2 4 2 2 5 3 1 3 2 0 1 2 1 1 3 3 5 1 4 2 2 5 2 4 1 1 1 1
 3 4 3 2 3 2 4 2 2 3 3 2 4 4 2 3 0 2 2 0 3 1 4 4 3 1 0 3 2 1 1 2 0 0 0 1 1
 1 1 1 3 1 1 1 1 3 1 0 2 2 1 2 2 1 2 0 2 3 3 2 5 0 4]

6.13. Exercise 8#

Write a function that uses a dictionary to “bin” a sample of size \(n\) drawn from a poisson distribution with rate \(\lambda\).

6.14. Solution 8#

def bin(n,lam) :
    D = {}
    S = numpy.random.poisson(lam,n)
    for s in S :
        if s not in D:
            D[s] = 1
        else:
            D[s] += 1
    return D

bin(10,3)
{1: 4, 4: 1, 0: 1, 2: 2, 6: 1, 5: 1}