第六章作業
6-1 人 : 使用一個字典來存儲一個熟人的信息, 包括名、 姓、 年齡和居住的城市。
該字典應包含鍵first_name 、 last_name 、 age 和city 。 將存儲在該字典中的每項信息都打印出來。
6-2 喜歡的數字 : 使用一個字典來存儲一些人喜歡的數字。 請想出5個人的名字, 並將這些名字用作字典中的鍵;
想出每個人喜歡的一個數字, 並將這些數字作為值存
儲在字典中。 打印每個人的名字和喜歡的數字。 為讓這個程序更有趣, 通過詢問朋友確保數據是真實的。
6-3 詞匯表 : Python字典可用於模擬現實生活中的字典, 但為避免混淆, 我們將后者稱為詞匯表。
想出你在前面學過的5個編程詞匯, 將它們用作詞匯表中的鍵, 並將它們的含義作為值存儲在詞匯表中。
以整潔的方式打印每個詞匯及其含義。 為此, 你可以先打印詞匯, 在它后面加上一個冒號, 再打印詞匯的含義; 也可在一行打印詞匯,
再使用換行符(\n ) 插 入一個空行, 然后在下一行以縮進的方式打印詞匯的含義。
答:6-1
friend = {'first_name ':'cao', 'last_name':'fu','age':'25','city':'hefei'} print(friend['first_name ']) print(friend['last_name']) print(friend['age']) print(friend['city']) 輸出: cao fu 25 hefei
6-2
name = {'ali':'1', 'taisen':'2','wo':'4','ta':'5','ni':'3'} print(name['ali']) print(name['taisen']) print(name['wo']) print(name['ta']) print(name['ni']) for ren,number in name.items():#左邊 定義為name,右邊定義位number print(ren.title() + "'s favorite number is " + str(number)) 輸出; Ali's favorite number is 1 Taisen's favorite number is 2 Wo's favorite number is 4 Ta's favorite number is 5 Ni's favorite number is 3
6-3
vocabulary = {'列表':'值可改','元組':'值不可改'} for name,meaning in vocabulary.items(): print('\n' + name + ':' + meaning) 輸出: 列表:值可改 元組:值不可改
6-4 詞匯表2 : 既然你知道了如何遍歷字典, 現在請整理你為完成練習 6-3而編寫的代碼, 將其中的一系列print 語句替換為一個遍歷字典中的鍵和值的循環。 確定該
循環正確無誤后, 再在詞匯表中添加5個Python術語。 當你再次運行這個程序時, 這些新術語及其含義將自動包含在輸出中。
6-5 河流 : 創建一個字典, 在其中存儲三條大河流及其流經的國家。 其中一個鍵—值對可能是' nile' : ' egypt' 。
使用循環為每條河流打印一條消息, 如“The Nile runs through Egypt.”。
使用循環將該字典中每條河流的名字都打印出來。
使用循環將該字典包含的每個國家的名字都打印出來。
6-6 調查 : 在6.3.1節編寫的程序favorite_languages.py中執行以下操作。
創建一個應該會接受調查的人員名單, 其中有些人已包含在字典中, 而其他人未包含在字典中。
遍歷這個人員名單, 對於已參與調查的人, 打印一條消息表示感謝。 對於還未參與調查的人, 打印一條消息邀請他參與調查。
6-4 略
6-5
county_rivers ={' nile' : ' egypt','changjiang':'china','yamaxun':'baxi'} for county,rivers in county_rivers.items(): print('“The '+ county + ' runs through ' + rivers + '.”') for county in county_rivers.keys(): print(county) for rivers in county_rivers.values(): print(rivers) 輸出: “The nile runs through egypt.” “The changjiang runs through china.” “The yamaxun runs through baxi.” nile changjiang yamaxun egypt china baxi
6-6
favorite_languages = { ' jen' : ' python' , ' sarah' : ' c' , ' edward' : ' ruby' , ' phil' : ' python' , } good_languages = {' jen' : ' python' ,' sarah' : ' c' ,} for name in favorite_languages.keys(): print(name. title()) if name in good_languages: print(name + '感謝你參見調查') else: print(name + "請你參加調查") 輸出; Jen jen感謝你參見調查 Sarah sarah感謝你參見調查 Edward edward請你參加調查 Phil phil請你參加調查
6-7 人 : 在為完成練習 6-1而編寫的程序中, 再創建兩個表示人的字典, 然后將這三個字典都存儲在一個名為people 的列表中。 遍歷這個列表, 將其中每個人的所有
信息都打印出來。
6-8 寵物 : 創建多個字典, 對於每個字典, 都使用一個寵物的名稱來給它命名; 在每個字典中, 包含寵物的類型及其主人的名字。 將這些字典存儲在一個名為pets
的列表中, 再遍歷該列表, 並將寵物的所有信息都打印出來。
6-9 喜歡的地方 : 創建一個名為favorite_places 的字典。 在這個字典中, 將三個人的名字用作鍵; 對於其中的每個人, 都存儲他喜歡的1~3個地方。 為讓這個練
習更有趣些, 可讓一些朋友指出他們喜歡的幾個地方。 遍歷這個字典, 並將其中每個人的名字及其喜歡的地方打印出來。
6-10 喜歡的數字 : 修改為完成練習 6-2而編寫的程序, 讓每個人都可以有多個喜歡的數字, 然后將每個人的名字及其喜歡的數字打印出來。
6-11 城市 : 創建一個名為cities 的字典, 其中將三個城市名用作鍵; 對於每座城市, 都創建一個字典, 並在其中包含該城市所屬的國家、 人口約數以及一個有關該
城市的事實。 在表示每座城市的字典中, 應包含country 、 population 和fact 等鍵。 將每座城市的名字以及有關它們的信息都打印出來。
6-12 擴展 : 本章的示例足夠復雜, 可以以很多方式進行擴展了。 請對本章的一個示例進行擴展: 添加鍵和值、 調整程序要解決的問題或改進輸出的格式。
6-7
people = {'caofu':{'first_name ':'cao','last_name':'fu','age':'25','city':'hefei'}, 'wangshi':{'first_name ':'wang','last_name':'shi','age':'64','city':'shenzhen'}, 'mayun':{'first_name ':'ma','last_name':'yun','age':'46','city':'haizhou'}} for username,user_info in people.items(): print("\nUsername: " + username) full_name = user_info['first_name '] + " " + user_info['last_name'] city = user_info['city'] print("\tFull name: " + full_name.title()) print("\tLocation: " + city.title()) 輸出: Username: caofu Full name: Cao Fu Location: Hefei Username: wangshi Full name: Wang Shi Location: Shenzhen Username: mayun Full name: Ma Yun Location: Haizhou
6-8
lili = {'type':'dog','owner':'ergou'} qq = {'type':'cat','owner':'qiqi'} pets =[lili,qq] for pet in pets: print(pet)#題目意思不要理解錯 輸出; {'type': 'dog', 'owner': 'ergou'} {'type': 'cat', 'owner': 'qiqi'}
6-9
favorite_places = { 'ergou':['beijing','xi an'], 'danliang':['shanghai'], 'shitou':['dali','lasa','chongqing'], } for k,v in favorite_places.items(): print('\n'+ k + "'s favorite places :") for city in v: print(city) 輸出;
6-10
favorite_numbers = { 'ergou':[8,1,3], 'qiqi':[6], 'pengpeng':[7,6,9,0], 'tiezhu':[4,5], 'dongzi':[2], } for k,v in favorite_numbers.items(): print('\n'+ k + "'s favorite numbers :") for number in v: print(str(number)) 輸出:
6-11
cities = {'beijing':{'country':'china','population':'*million','fact':'The political economic and cultural center'}, 'New York':{'country':'USA','population':'*million','fact':'International metropolis'}, 'Singapore':{'country':'singapore','population':'*million','fact':'city-state'} } for k,v in cities.items(): print('\n' + k.title()) for ks,vs in v.items(): print(ks + ' : ' + vs) 輸出:
6-12 略