Docsity

DIccionarios, Apuntes de Ingeniería Biomédica

Toggle sidebar
bg1
Unit 7. Dictionaries
Goals
•Define and use dictionaries
•Access the values stored in them
•Manage lists of dictionaries
Dictionaries
Python dictionaries store information associating each data to a value (called key, in bold
in the example). Here we will use text values as keys, but you can use other types too.
dictio = {'name':'Owen', 'phone':324551532, 'age':25}
In order to access the values in a dictionary, you have to give the key in brackets and
quotes (if it is a text variable):
print(dictio['name'])
Dictionaries are dynamic structures, therefore it is possible to add new elements after
creating them. An alternate way of making a dictionary is as follows:
# Empty dictionary
dictio = { }
dictio['name'] = 'Owen'
dictio['phone'] = 324551532
dictio['age'] = 25
# You can also read the values from the keyboard
dictio['name'] = input('Type your name:')
...
Lists of dictionaries
Often you will want to store several dictionaries in a single data structure. Lists are the
usual solution to this. They are very similar to vectors but they can store any data type
and it is easy to add new elements to them.
# Empty list
myList = [ ]
# Add dictionaries of geometrical shapes
myList.append({'shape':'square' , 'color':'red' })
myList.append({'shape':'triangle', 'color':'yellow'})
myList.append({'shape':'circle' , 'color':'blue' })
Lists are traversed in the same way as vectors, with a for that accesses each of its
elements (i.e. each dictionary in this case):
Unidad 7 Computer Science Lab - EUETIB - UPC 1
Samir Kanaan Izquierdo - 2014
pf2

Vista previa parcial del texto

¡Descarga DIccionarios y más Apuntes en PDF de Ingeniería Biomédica solo en Docsity!

Unit 7. Dictionaries

Goals

  • Define and use dictionaries
  • Access the values stored in them
  • Manage lists of dictionaries

Dictionaries

Python dictionaries store information associating each data to a value (called key , in bold in the example). Here we will use text values as keys, but you can use other types too. dictio = {' name ':'Owen', ' phone ':324551532, ' age ':25} In order to access the values in a dictionary, you have to give the key in brackets and quotes (if it is a text variable): print(dictio['name']) Dictionaries are dynamic structures, therefore it is possible to add new elements after creating them. An alternate way of making a dictionary is as follows:

Empty dictionary

dictio = { } dictio['name'] = 'Owen' dictio['phone'] = 324551532 dictio['age'] = 25

You can also read the values from the keyboard

dictio['name'] = input('Type your name:') ...

Lists of dictionaries

Often you will want to store several dictionaries in a single data structure. Lists are the usual solution to this. They are very similar to vectors but they can store any data type and it is easy to add new elements to them.

Empty list

myList = [ ]

Add dictionaries of geometrical shapes

myList.append({'shape':'square' , 'color':'red' }) myList.append({'shape':'triangle', 'color':'yellow'}) myList.append({'shape':'circle' , 'color':'blue' }) Lists are traversed in the same way as vectors, with a for that accesses each of its elements (i.e. each dictionary in this case): Unidad 7 Computer Science Lab - EUETIB - UPC 1 Samir Kanaan Izquierdo - 2014

Following the previous example

for piece in myList: print(piece['shape'], piece['color']) You can also traverse the list by indices, and perform searches on it with while.

Exercise 1: rice

Write a program that reads the data of two rice batches (variety, caliber, area) and stores them in two dictionaries. It should compare them and tell if they are equal or, if there are any differences, what are these differences (in variety, caliber or area).

Exercise 2: geometrical center

Write a program that reads the coordinates of three points in space (x, y, z), each one on a dictionary. Then it should compute its geometrical center and it should store it in another dictionary.

Exercise 3: calendar I

Write a program that reads the appointments of a calendar during a week, with a dictionary for each appointment (day number, time of day, place, person). Show everything on screen.

Exercise 4: calendar II

With the calendar from the previous exercise, the program should ask the name of a person and print the appointments with that person.

Exercise 5: calendar III

Now the program should ask a day number and print how many appointments are scheduled for that day.

Exercise 6: calendar IV

The program should ask a place name and print if there are any appointments in that place or not. Unidad 7 Computer Science Lab - EUETIB - UPC 2 Samir Kanaan Izquierdo - 2014