Dictionaries · Creating Dictionaries · Practice Question
Unique Keys
Question
Create a dictionary with:
- 10 unique long keys
- Keys randomly selected from the integers between 0 and 99
- 10 random float values between 0 and 100
Solution
d:(-10?100)!10?100.0Example result:
21| 42.37631
87| 91.84272
3 | 17.46392
64| 55.1208
9 | 6.945217
38| 73.50618
72| 28.39471
14| 81.6274
56| 49.01822
95| 12.77483Explanation
-10?100 rolls 10 unique longs without replacement from 0–99 (the keys); 10?100.0 rolls 10 floats with replacement (the values). ! pairs them into the dictionary.
The negative left argument is what guarantees unique keys. Note that without-replacement sampling requires the domain to be at least as large as the count requested — -10?8 fails with 'length since only 8 distinct values exist for 10 requested keys.