新南威尔士大学COMP9021 QUIZ1课业解析
题意:
巩固复习python中字典的相关知识
解析:
题目要求有4点,1补全print语句,统计mapping字典中元素的个数 ,2统计mapping字典中,没有值的键,存储在nonkeys列表中 ,3统计mapping字典中所有元素的值,存储在mapping_as_a_list列表中,4统计mapping字典中值唯一的元素,存储在one_to_one_part_of_mapping字典中 。
spilt()函数对用户输入的两个数按空格切分,arg_for_seed和upper_bound分别对应用户输入的第一和第二个数+1。
for i in range(1, upper_bound)循环表示遍历mapping字典的元素(第一个元素除外)并赋值,每次循环调用randrange()函数在规定范围内产生一个随机数,如果是正数则把它赋值给对应的字典元素。 统计字典元素个数可用len(mapping),第二问和第三问则是对字典元素的操作,第四问则是列举一一映射的字典
涉及知识点:
python字典,列表
更多可加微信讨论
微信号:Alexa_au
QUIZ 1
COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_1.py
Enter two integers: 0 4
The generated mapping is:
{3: 1}
The mappings's so-called "keys" make up a set whose number of elements is 1.
The list of integers between 1 and 4 that are not keys of the mapping is:
[1, 2, 4]
Represented as a list, the mapping is:
[None, None, None, 1, None]
The one-to-one part of the mapping is:
{3: 1}
$ python3 quiz_1.py
Enter two integers: 0 8
The generated mapping is:
{2: 4, 3: 8, 4: 7, 5: 7}
The mappings's so-called "keys" make up a set whose number of elements is 4.
The list of integers between 1 and 8 that are not keys of the mapping is:
[1, 6, 7, 8]
Represented as a list, the mapping is:
[None, None, 4, 8, 7, 7, None, None, None]
The one-to-one part of the mapping is:
{2: 4, 3: 8}
Date: Term 3, 2019.
$ python3 quiz_1.py | |
Enter two integers: 10 10 | |
The generated mapping is: | |
{1: 8, 2: 8, 3: 8, 4: 10, 7: 10, 8: 9} | |
The mappings's so-called "keys" make up a set whose number of elements is 6. | |
The list of integers between 1 and 10 that are not keys of the mapping is: | |
[5, 6, 9, 10] | |
Represented as a list, the mapping is: | |
[None, 8, 8, 8, 10, None, None, 10, 9, None, None] | |
The one-to-one part of the mapping is: | |
{8: 9} | |
$ python3 quiz_1.py | |
Enter two integers: 5 15 | |
The generated mapping is: | |
{1: 10, 3: 7, 8: 13, 9: 10, 10: 7, 11: 15, 12: 3, 13: 2} | |
The mappings's so-called "keys" make up a set whose number of elements is 8. | |
The list of integers between 1 and 15 that are not keys of the mapping is: | |
[2, 4, 5, 6, 7, 14, 15] | |
Represented as a list, the mapping is: | |
[None, 10, None, 7, None, None, None, None, 13, 10, 7, 15, 3, 2, None, None] | |
The one-to-one part of the mapping is: | |
{8: 13, 11: 15, 12: 3, 13: 2} | |
$ python3 quiz_1.py | |
Enter two integers: 23 16 | |
The generated mapping is: | |
{1: 13, 2: 3, 3: 9, 9: 12, 10: 12, 12: 13, 13: 15, 15: 16, 16: 13} | |
The mappings's so-called "keys" make up a set whose number of elements is 9. | |
The list of integers between 1 and 16 that are not keys of the mapping is: | |
[4, 5, 6, 7, 8, 11, 14] | |
Represented as a list, the mapping is: | |
[None, 13, 3, 9, None, None, None, None, None, 12, 12, None, 13, 15, None, 16, 13] | |
The one-to-one part of the mapping is: | |
{2: 3, 3: 9, 13: 15, 15: 16} |