SEE1002 Introduction To Computing For Energy and Environment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SEE1002 Introduction to Computing for Energy and

Environment

Lab 3.4: Derived data structures

1. List of lists
We can nest arbitrarily many lists inside a 1-D list A 3-D list is obtained by nesting a 2-D list inside a regular 1-D
list.

Example 1: a 3-D list of lists

In [3]:

1# Example 1a: a 3-D list of lists (version 1)


2list2d_1 = [ [0,0], [1,1] ]
3list2d_2 = [ [2,2], [3,3] ]
4list3d = [list2d_1, list2d_2] # note that list2d_1 and list2d_2 need to be defined
5
6print('list3d=',list3d) # this is the entire 3-D list
7print('list3d[0]=',list3d[0]) # this is the first row
8print('list3d[0][0]=',list3d[0][0]) # this is the first list of the first row
9

list3d= [[[0, 0], [1, 1]], [[2, 2], [3, 3]]]


list3d[0]= [[0, 0], [1, 1]]
list3d[0][0]= [0, 0]

In [5]:

1# Example 1a: a 3-D list of lists (version 2)


2
3list3d = [ [ [0,0], [1,1]], [ [2,2], [3,3] ] ] # this is equivalent to version
4print('list3d=',list3d) # this is the entire 3-D list
5print('list3d[0]=',list3d[0]) # this is the first row
6print('list3d[0][0]=',list3d[0][0]) # this is the first list of the first row
7

list3d= [[[0, 0], [1, 1]], [[2, 2], [3, 3]]]


list3d[0]= [[0, 0], [1, 1]]
list3d[0][0]= [0, 0]

Exercise 1 Consider the list, list3d = [ [[1,2,3], [4,5,6], [7,8,9]],


[[10,12,13], [14,15,16], [17,18,19]],
[[20,22,23], [24,25,26], [27,28,29]]
].

What are the values of

list3d
list3d[0]
list3d[0][0]
list2d[0][0][1]

In [1]:

1 # Answer to Exercise 1
2
3

Exercise 2: Student grades

Three students have the following grades:

1. A, A-,B,B-,B
2. B,B,C-,C+,C
3. C,C-,C,D,D

Store their grades using 3 lists and a single list of lists.

In [2]:

1 # Answer to Exercise 2
2
3

Exercise 3: building a list of lists

A list of lists can be used to create an array, i.e. a data structure composed of elements of the same data type.

Store the values of 𝑓(𝑥,𝑦) = cos(𝑥)sin(𝑦) over the domain [0,2𝜋] × [0,2𝜋]
using a 2-D list of lists.
Consider 𝑁 = 10 uniform spaced points in and 𝑥 𝑁 = 10 uniform spaced points in . 𝑦
Store the values in the form

a) list2d[y][x]

b) list2d[x][y] .

Compare the results for list2d[0] and list2d[N-1] and confirm that they agree with the function
definition.

In [6]:

1 # Answer to Exercise 3a: # values stored in form list2d[y][x]


2
3
4

In [25]:

1 # Answer to Exercise 3b: # values stored in form list2d[x][y]


2
3
4
2 List of dictionaries
A list of dictionaries is useful when we have many items (e.g. students in a class) and unstructured information
about them (e.g. personal info.

Example 2: customer database

In [19]:

1 # example 2
2
3 customer0_dict = {'name': 'Chan',
4 'telephone': 12345678,
5 'city': 'Kowloon',
6 'total_purchase': 0}
7
8 customer1_dict = {'name':'Lee',
9 'telephone': 23456789,
10 'city':'New Territories',
11 'total_purchase': 998}
12
13 customer2_dict = {'name': 'Wong',
14 'telephone': 34567890,
15 'city':'Hong Kong',
16 'total_purchase': 9998}
17
18 customers = [customer0_dict, customer1_dict, customer2_dict]
19
20 for i,clist in enumerate(customers):
21 print('The name of Customer {:d} is {:s}'.format(i,clist['name']))
22
23 print()
24 total=0
25 for i,clist in enumerate(customers):
26 print('Customer {:d} purchased {:d}'.format(i,clist['total_purchase']))
27 total += clist['total_purchase']
28 print('The total purchased = ',total)

The name of Customer 0 is Chan


The name of Customer 1 is Lee
The name of Customer 2 is Wong

Customer 0 purchased 0
Customer 1 purchased 998
Customer 2 purchased 9998
The total purchased = 10996

Exercise 4: Student info

Consider the following table of student info:

sid surname grades

1234 Chan A,A-,B,B-,B

2345 Lee B,B,C-,C+,C

3456 Wong C,C-,C,D,D

Summarise this information using a list of dictionaries, students. What is


students[0]?
students[0]['sid']?
students[1]['grades']?
students[2]['grades'][2]?

In [4]:

1 # Answer to Exercise 4
2
3

3. Dictionary of lists
A dictionary of lists provides detailed information about different properties. It is complementary to the list of
dictionaries.

Example 3: Customer database revisited

In [24]:

1 # example 2
2
3 name_list = ['Chan', 'Lee', 'Wong']
4 telephone_list = [12345678, 23456789, 34567890]
5 city_list = ['Kowloon', 'New Territories', 'Hong Kong']
6 purchase_list = [0, 998, 9998]
7
8 customer_dict = {'name': name_list,
9 'telephone': telephone_list,
10 'city': city_list,
11 'total': purchase_list
12 }
13
14 clist = customer_dict['name']
15 for i,name in enumerate(clist):
16 print('The name of Customer {:d} is {:s}'.format(i,name))
17
18 print()
19 total=0
20 dlist = customer_dict['total']
21 for i, purchased in enumerate(dlist):
22 print('Customer {:d} purchased {:d}'.format(i,purchased))
23 total += purchased
24 print('The total purchased = ',total)

The name of Customer 0 is Chan


The name of Customer 1 is Lee
The name of Customer 2 is Wong

Customer 0 purchased 0
Customer 1 purchased 998
Customer 2 purchased 9998
The total purchased = 10996

Exercise 5: Student info revisited

Consider once more the table from Exercise 4. Summarise this information using a dictionary of lists, info.
Consider once more the table from Exercise 4. Summarise this information using a dictionary of lists, info.
What is

info['sid']?
info['sid'][0]?
info['grades'][1]?
info['surname'][2]?

In [ ]:

1 # Answer to Exercise 5
2
3

You might also like