Ex 2: Manage your Data
In this session, you will learn a new collection type: dictionary. It is very helpful for you to manage your data.
Ex 2.1: Cable-net Information
Question:
While digging deep into the design and fabrication, you start to accumulating information about the cables, including the length, dimension, cable stress, etc. How would you hold your information?
cable | length | dimension | stress |
1 | 1.6 m | 2 mm | 275 N |
2 | 3.6 m | 2 mm | 185 N |
3 | 2.4 m | 2 mm | 105 N |
4 | 3.4 m | 2 mm | 134 N |
5 | 2.7 m | 2 mm | 155 N |
6 | 2.8 m | 2 mm | 265 N |
7 | 3.3 m | 2 mm | 150 N |
8 | 3.1 m | 2 mm | 185 N |
9 | 3.7 m | 2 mm | 124 N |
10 | 1.8 m | 2 mm | 234 N |
11 | 1.8 m | 2 mm | 259 N |
12 | 2.6 m | 2 mm | 201 N |
Answer:
In this case, we can create three lists, containing information of length, dimension, and stress respectively. However, this solution is not so efficient and clear, especially when our data set is big. Here we would introduce a new collection type, dictionary.
First, let's create a dictionary to save all the lengths of the cables. The key
is the index of the cable, and the item
is the length.
{0: 1.6, 1: 3.6, 2: 2.4, 3: 3.4, 4: 2.7, 5: 2.8, 6: 3.3, 7: 3.1, 8: 3.7, 9: 1.8, 10: 1.8, 11: 2.6}
Now let's save more information.
{0: {'length': 1.6, 'dimension': 2, 'stress': 275}, 1: {'length': 3.6, 'dimension': 2, 'stress': 185}, 2: {'length': 2.4, 'dimension': 2, 'stress': 105}, 3: {'length': 3.4, 'dimension': 2, 'stress': 134}, 4: {'length': 2.7, 'dimension': 2, 'stress': 155}, 5: {'length': 2.8, 'dimension': 2, 'stress': 265}, 6: {'length': 3.3, 'dimension': 2, 'stress': 150}, 7: {'length': 3.1, 'dimension': 2, 'stress': 185}, 8: {'length': 3.7, 'dimension': 2, 'stress': 124}, 9: {'length': 1.8, 'dimension': 2, 'stress': 234}, 10: {'length': 1.8, 'dimension': 2, 'stress': 259}, 11: {'length': 1.8, 'dimension': 2, 'stress': 201}
We could easily find the information on a specific cable. For example, let's find the information on cable 3.
{'length': 3.4, 'dimension': 2, 'stress': 134}
if we find the stress of cable 3 is incorrect, we could modify it.
{'length': 3.4, 'dimension': 2, 'stress': 154}
Ex 2.2: Calculate the volume and Weight
Question 1:
You have a dictionary containing the surface area and thickness of the voussoir. Now you want to calculate the volume of each piece (volume = area * thickness), and add the number to the dictionary.
Answer:
Voussior 0 volume: 132090
Voussior 1 volume: 157320
Voussior 2 volume: 141120
Voussior 3 volume: 159980
Voussior 4 volume: 225620
Voussior 5 volume: 240000
Question 2:
Now you decide the voussoir would be cut from limestone, and its density is 2160 kg/m3, could you calculate the weight of each piece and add it to the dictionary?
Answer:
Last updated