Section B.2 Dictionaries
Subsection B.2.1 Colors of Fruits
In Python and SageMath, a dictionary is a convenient data structure for establishing a relationship between sets of data. From the point of view of this text, we can think of a dictionary as a concrete realization of a relation between two sets or on a single set. A dictionary resembles a function in that there is a set of data values called the
keys
, and for each key, there is a value
. The value associated with a key can be almost anything, but it is most commonly a list.To illustrate the use of dictionaries, we will define a relationship between colors and fruits. The keys will be a set of colors and values associated with each color will be a list of fruits that can take on that color. We will demonstrate how to initialize the dictionary and how to add to it. The following series of assignments have no output, so we add a print statement to verify that this cell is completely evaluated.
xxxxxxxxxx
fruit_color={}
fruit_color['Red']=['apple','pomegranate','blood orange']
fruit_color['Yellow']=['banana','apple','lemon']
fruit_color['Green']=['apple','pear','grape','lime']
fruit_color['Purple']=['plum','grape']
fruit_color['Orange']=['orange','pineapple']
print('done')
We distinguish a color from a fruit by capitalizing colors but not fruit. The keys of this dictionary are the colors. The
keys()
method returns an interator; so to get a list of keys we wrap the result with list()
.xxxxxxxxxx
list(fruit_color.keys())
As an afterthought, we might add the information that a raspberry is red as follows. You have to be careful in that if βRedβ isnβt already in the dictionary, it doesnβt have a value. This is why we need an if statement.
xxxxxxxxxx
if 'Red' in fruit_color:
fruit_color['Red']=fruit_color['Red']+['raspberry']
else:
fruit_color['Red']=['raspberry']
fruit_color['Red']
A dictionary is iterable, with an iterator taking on values that are the keys. Here we iterate over our dictionary to output lists consisting of a color followed by a list of fruits that come in that color.
xxxxxxxxxx
for color in fruit_color:
print([color,fruit_color[color]])
We can view a graph of this relation between colors and fruits, but the default view is a bit unconventional.
xxxxxxxxxx
DiGraph(fruit_color).plot()
With a some additional coding we can line up the colors and fruits in their own column. First we set the positions of colors on the left with all -coordinates equal to -5 using another dictionary called
vertex_pos
.xxxxxxxxxx
vertex_pos={}
k=0
for c in fruit_color.keys():
vertex_pos[c]=(-5,k)
k+=1
vertex_pos
Next, we place the fruit vertices in another column with -coordinates all equal to 5. In order to do this, we first collect all the fruit values into one set we call
fruits
.xxxxxxxxxx
fruits=Set([ ])
for v in fruit_color.values():
fruits=fruits.union(Set(v))
k=0
for f in fruits:
vertex_pos[f]=(5,k)
k+=1
vertex_pos
Now the graph looks like a conventional graph for a relation between two different sets. Notice that itβs not a function.
xxxxxxxxxx
DiGraph(fruit_color).plot(pos=vertex_pos,vertex_size=1)
You have attempted 1 of 1 activities on this page.