Dicts¶
Indices and tables¶
Background¶
Like lists, dictionaries store a collection of data. Adding and retrieving entries is easy and quick if you have the key. However unlike lists, dictionaries cannot be traversed at all by index. Fortunately, there are functions like ‘dict.keys()’, ‘dict.items()` and ‘dict.values()’ that return iterators for use in loops.
Exercise¶
Sensor data from sensors mounted to several DUTs was collected and is listed in Table 14. As is common with real-world data, the measurements are not necessarily continuous. Use this data to complete the following exercises.
DUT Name | Sensor-A Samples | Sensor-B Samples |
---|---|---|
CHZ | 192, 4, 1, 54 | 228, 147, 61, 84 |
RXJ | 33 | 48, 41, 910 |
AMP | 292, 12, 84, 991 | 28901 |
- For each row in the table create a single dictionary. Use the sensor name string as the key (ie: “sensor-A”).
- You noticed that you forgot to include some samples for sensor-A of RXJ. Extend the sample list for that sensor with the following samples: 55, 21
- AMP sensor-a, sample 2 is incorrect. Instead of being 84, it should have been 94. Correct the sample list for that sensor.
- Create an empty dictionary and add each of the DUT dictionaries as separate entries. Use the DUT Name as the key.
- CHZ sensor-b, samples 2 and 3 are incorrect, they should be, 71 and 94 respectively. Correct the sample list for that sensor.
- Find the count of, min, max and sum of the values in the sample set. This will require accessing the dictionaries within the dictionary.
- Determine if the value 71 occurs in any sample set. Is it easier to do on lists or dictionaries?
- Confirm the value 60 is not in the sample set.
- Determine how many times the value of 228 occurs in the sample set.
- Sort the sample of each sensor set from largest to smallest.
- Insert two new rows to the table of data with the following criteria:
DUT Name: “BMX”Sensor-A Samples: 1, 4, 6, 735Sensor-B samples: 12, 135, 2462DUT Name: “QRT”Sensor-A Samples: 2, 31, 52, 85Sensor-B samples: 899, 900, 901
- You decided that the AMP data is no good. Remove it from the table.
- Extract all the DUT names from the sample table rows as a list.
Hints¶
- None