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.

cable_length_list = [1.6, 3.6, 2.4, 3.4, 2.7, 2.8, 3.3, 3.1, 3.7, 1.8, 1.8, 2.6]

# create an empty dictionary
cables_dict = {}

for i in range(len(cable_length_list)):
    cables_dict[i] = cable_length_list[i]

print(cables_dict)

{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.

cable_length_list = [1.6, 3.6, 2.4, 3.4, 2.7, 2.8, 3.3, 3.1, 3.7, 1.8, 1.8, 2.6]
cable_stress_list = [275, 185, 105, 134, 155, 265, 150, 185, 124, 234, 259, 201]

# create an empty dictionary
# key is index of the cable, item is infomation of the cable
cables_dict = {}

for i in range(len(cable_length_list)):
    # for each cable, we create another dictionary
    # key: category, item: value of the category
    cables_dict[i] = {}
    cables_dict[i]["length"] = cable_length_list[i]
    cables_dict[i]["dimension"] = 2
    cables_dict[i]["stress"] = cable_stress_list[i]

print(cables_dict)

{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.

print(cables_dict[3])

{'length': 3.4, 'dimension': 2, 'stress': 134}

if we find the stress of cable 3 is incorrect, we could modify it.

cables_dict[3]["stress"] = 154
print(cables_dict[3])

{'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.

# vault dictionary
vault = {
    0: {"area": 2590, "thickness": 51},
    1: {"area": 3420, "thickness": 46},
    2: {"area": 2940, "thickness": 48},
    3: {"area": 4210, "thickness": 38},
    4: {"area": 3890, "thickness": 58},
    5: {"area": 4000, "thickness": 60}
}

Answer:

# iterate through the dictionary
for key in vault.keys():
    # get the area and thickness of the voussoir
    v_area = vault[key]["area"]
    v_thickness = vault[key]["thickness"]
    # calculate the volume
    v_volume = round(v_area * v_thickness, 2)
    print("Voussior", key, "volume:", v_volume)
    # add the volume to the dictionary
    vault[key]["volume"] = v_volume

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:

density = 2160  # kg/m3

# iterate through the dictionary
for key in vault.keys():
    # get the area and thickness of the voussoir
    v_area = vault[key]["area"]
    v_thickness = vault[key]["thickness"]
    # calculate the volume and weight
    v_volume = v_area * v_thickness
    v_weight = v_volume * density / 1000 ** 3
    v_weight = round(v_weight, 2)  # limit the number to 2 decimals
    print("Voussior", key, "volume:", v_volume, "weight:", v_weight)
    # add the volume to the dictionary
    vault[key]["volume"] = v_volume
    vault[key]["weight"] = v_weight

Last updated