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.

Table 14 Table of Data for Dict 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
  1. For each row in the table create a single dictionary. Use the sensor name string as the key (ie: “sensor-A”).
  2. 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
  3. AMP sensor-a, sample 2 is incorrect. Instead of being 84, it should have been 94. Correct the sample list for that sensor.
  4. Create an empty dictionary and add each of the DUT dictionaries as separate entries. Use the DUT Name as the key.
  5. CHZ sensor-b, samples 2 and 3 are incorrect, they should be, 71 and 94 respectively. Correct the sample list for that sensor.
  6. Find the count of, min, max and sum of the values in the sample set. This will require accessing the dictionaries within the dictionary.
  7. Determine if the value 71 occurs in any sample set. Is it easier to do on lists or dictionaries?
  8. Confirm the value 60 is not in the sample set.
  9. Determine how many times the value of 228 occurs in the sample set.
  10. Sort the sample of each sensor set from largest to smallest.
  11. Insert two new rows to the table of data with the following criteria:
DUT Name: “BMX”
Sensor-A Samples: 1, 4, 6, 735
Sensor-B samples: 12, 135, 2462
DUT Name: “QRT”
Sensor-A Samples: 2, 31, 52, 85
Sensor-B samples: 899, 900, 901
  1. You decided that the AMP data is no good. Remove it from the table.
  2. Extract all the DUT names from the sample table rows as a list.

Hints

  • None

Solution

When you are ready to see one possible solution, download and open this Jupyter Notebook file