新南威爾士大學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字典,串列
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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/22566.html
標籤:語言基礎/算法/系統設計
