Dictionaries#

Dictionaries are data structures that hold key-value pairs, written as key:value.

See also

You can define a dictionary like this:

image_metadata_dictionary = {'x_resolution': 0.25, 'y_resolution': 0.25, 'z_resolution': 1.0, 'voxel_physical_unit': 'um'}

For readers convenience, consider writing them like this:

image_metadata_dictionary = {
    'x_resolution': 0.25, 
    'y_resolution': 0.25, 
    'z_resolution': 1.0,
    'voxel_physical_unit': 'um'
    }
image_metadata_dictionary

If you want to access a given entry in the dictionary, you can address it using square brackets and the key:

image_metadata_dictionary['x_resolution']

You can add elements to the dictionary:

image_metadata_dictionary['fps'] = 60
image_metadata_dictionary

You can also retrieve a list of all keys in the dictionary:

keys = list(image_metadata_dictionary.keys())
keys
keys[1]

Tables#

Tables can be expressed as dictionaries with arrays as elements.

measurements_week = {
    'Monday':   [2.3, 3.1, 5.6],
    'Tuesday':  [1.8, 7.0, 4.3],
    'Wednesday':[4.5, 1.5, 3.2],
    'Thursday': [1.9, 2.0, 6.4],
    'Friday':   [4.4, 2.3, 5.4]
}
measurements_week
measurements_week['Monday']

You can also store variables in such tables:

w1 = 5
h1 = 3
area1 = w1 * h1

w2 = 2
h2 = 4
area2 = w2 * h2

rectangles = {
    "width": [w1, w2],
    "height": [h1, h2],
    "area": [area1, area2]
}
rectangles

You can use the pandas library to better visualize tables (also called dataframes).

import pandas as pd

rectangles_table = pd.DataFrame(rectangles)

rectangles_table

Exercise#

You just measured the radius of three circles. Write them into a table and add a column with corresponding circlea area measurements.

r1 = 12
r2 = 8
r3 = 15