SEE1002 Introduction To Computing For Energy and Environment
SEE1002 Introduction To Computing For Energy and Environment
SEE1002 Introduction To Computing For Energy and Environment
Environment
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.
In [3]:
In [5]:
list3d
list3d[0]
list3d[0][0]
list2d[0][0][1]
In [1]:
1 # Answer to Exercise 1
2
3
1. A, A-,B,B-,B
2. B,B,C-,C+,C
3. C,C-,C,D,D
In [2]:
1 # Answer to Exercise 2
2
3
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]:
In [25]:
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)
Customer 0 purchased 0
Customer 1 purchased 998
Customer 2 purchased 9998
The total purchased = 10996
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.
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)
Customer 0 purchased 0
Customer 1 purchased 998
Customer 2 purchased 9998
The total purchased = 10996
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