1. 使用字典 字典 是一系列鍵—值對(duì) 。每個(gè)鍵 都與一個(gè)值相關(guān)聯(lián),你可以使用鍵來(lái)訪問(wèn)與之相關(guān)聯(lián)的值。 字典用放在花括號(hào){} 中的一系列鍵—值對(duì)表示,鍵和值之間用冒號(hào)分隔,而鍵—值對(duì)之間用逗號(hào)分隔。
alien_0 = {'color': 'green', 'points': 5}(1).訪問(wèn)字典中的值 要獲取與鍵相關(guān)聯(lián)的值,可依次指定字典名和放在方括號(hào)內(nèi)的鍵
alien_0 = {'color': 'green', 'points': 5}new_points = alien_0['points'](2).添加鍵—值對(duì) 字典是一種動(dòng)態(tài)結(jié)構(gòu),可隨時(shí)在其中添加鍵—值對(duì)。 要添加鍵—值對(duì),可依次指定字典名、用方括號(hào)括起的鍵和相關(guān)聯(lián)的值。alien_0 = {'color': 'green', 'points': 5}print(alien_0)alien_0['x_position'] = 0alien_0['y_position'] = 25print(alien_0)(3).修改字典中的值 要修改字典中的值,可依次指定字典名、用方括號(hào)括起的鍵以及與該鍵相關(guān)聯(lián)的新值.
alien_0 = {'color': 'green'}print("The alien is " + alien_0['color'] + ".")alien_0['color'] = 'yellow'print("The alien is now " + alien_0['color'] + ".")(4).刪除鍵—值對(duì) 可使用del 語(yǔ)句將相應(yīng)的鍵—值對(duì)徹底刪除。
alien_0 = {'color': 'green', 'points': 5}print(alien_0)del alien_0['points']print(alien_0)2. 遍歷字典 (1).遍歷所有的鍵—值對(duì) 方法items(), 它返回一個(gè)鍵—值對(duì)列表。
user_0 = {'username': 'efermi','first': 'enrico','last': 'fermi',}for key, value in user_0.items(): print("/nKey: " + key) print("Value: " + value)(2).遍歷字典中的所有鍵 在不需要使用字典中的值時(shí),方法keys() 很有用
favorite_languages = {'jen': 'python','sarah': 'c','edward': 'ruby','phil': 'python',}for name in favorite_languages.keys(): print(name.title())(3).按順序遍歷字典中的所有鍵 字典總是明確地記錄鍵和值之間的關(guān)聯(lián)關(guān)系,但獲取字典的元素時(shí),獲取順序是不可預(yù)測(cè)的。 為此,可使用函數(shù)sorted() 來(lái)獲得按特定順序排列的鍵列表的副本.
favorite_languages = {'jen': 'python','sarah': 'c','edward': 'ruby','phil': 'python',}for name in sorted(favorite_languages.keys()): print(name.title() + ", thank you for taking the poll.")(4).遍歷字典中的所有值 可使用方法values() ,它返回一個(gè)值列表,而不包含任何鍵。
favorite_languages = {'jen': 'python','sarah': 'c','edward': 'ruby','phil': 'python',}print("The following languages have been mentioned:")for language in favorite_languages.values(): print(language.title())最終的列表可能包含大量的重復(fù)項(xiàng)。為剔除重復(fù)項(xiàng),可使用集合(set)。 集合 類似于列表,但每個(gè)元素都必須是獨(dú)一無(wú)二的.
favorite_languages = {'jen': 'python','sarah': 'c','edward': 'ruby','phil': 'python',}print("The following languages have been mentioned:")for language in set(favorite_languages.values()): print(language.title())3. 嵌套 將一系列字典存儲(chǔ)在列表中,或?qū)⒘斜碜鳛橹荡鎯?chǔ)在字典中,這稱為嵌套 (1). 字典列表
alien_0 = {'color': 'green', 'points': 5}alien_1 = {'color': 'yellow', 'points': 10}alien_2 = {'color': 'red', 'points': 15}aliens = [alien_0, alien_1, alien_2]for alien in aliens: print(alien)(2).在字典中存儲(chǔ)列表
# 存儲(chǔ)所點(diǎn)比薩的信息pizza = {'crust': 'thick','toppings': ['mushrooms', 'extra cheese'],}# 概述所點(diǎn)的比薩print("You ordered a " + pizza['crust'] + "-crust pizza " +"with the following toppings:")for topping in pizza['toppings']: print("/t" + topping)(3).在字典中存儲(chǔ)字典
users = { 'aeinstein': { 'first': 'albert', 'last': 'einstein', 'location': 'princeton', }, 'mcurie': { 'first': 'marie', 'last': 'curie', 'location': 'paris', },}for username, user_info in users.items(): print("/nUsername: " + username) full_name = user_info['first'] + " " + user_info['last'] location = user_info['location'] print("/tFull name: " + full_name.title()) print("/tLocation: " + location.title())新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注