這11個「Python字典」知識點 你不能不知道 | 您所在的位置:網(wǎng)站首頁 › 67年屬羊女用什么微信名好 › 這11個「Python字典」知識點 你不能不知道 |
哈嘍,大家好呀~ 今天給大家介紹一下[python]字典的11個知識點,這11個知識點你們不能不知道,趕緊收藏起來吧~ 關(guān)于Python字典,算是Python中相當(dāng)重要的數(shù)據(jù)類型了。在你學(xué)會基礎(chǔ)知識后,`字典`這個概念,將會伴隨著你后面的學(xué)習(xí)和工作。 因此,這里有幾個相當(dāng)重要的知識點,大家有必要知道。 一,字典是否是無序的關(guān)于這個概念,很多朋友不一定清楚。 在 Python 2.7 中,字典是無序的結(jié)構(gòu)。字典項目的順序是混亂的。這意味著項目的順序是確定性和可重復(fù)的。 代碼語言:javascript復(fù)制>>> # Python 2.7 >>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}![]() 在 Python 3.5 中,字典仍然是無序的,但這次是隨機(jī)的數(shù)據(jù)結(jié)構(gòu)。這意味著每次重新運行字典時,您都會得到不同的項目順序。 代碼語言:javascript復(fù)制>>> # Python 3.5 >>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}![]() 在 Python 3.6 更高版本中,字典是有序的數(shù)據(jù)結(jié)構(gòu),這意味著它們保持元素的順序與它們被引入時的順序相同。 代碼語言:javascript復(fù)制>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}![]() 假設(shè)您有一本字典,由于某種原因需要將鍵轉(zhuǎn)換為值,值轉(zhuǎn)換為鍵,應(yīng)該怎么做呢? 代碼語言:javascript復(fù)制>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {} >>> for key, value in a_dict.items(): ... new_dict[value] = key ... >>> new_dict {1: 'one', 2: 'two', 3: 'thee', 4: 'four'}![]() 有時候,你需要根據(jù)某種條件來過濾字典。那么配合if條件語句,是一個很好的選擇。 代碼語言:javascript復(fù)制>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {} # Create a new empty dictionary >>> for key, value in a_dict.items(): ... if value >> new_dict {'one': 1, 'two': 2}![]() 在Python中遍歷字典時。需要進(jìn)行一些計算也是很常見的。假設(shè)您已將公司銷售額的數(shù)據(jù)存儲在字典中,現(xiàn)在您想知道一年的總收入。 代碼語言:javascript復(fù)制>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> total_income = 0.00 >>> for value in incomes.values(): ... total_income += value # Accumulate the values in total_income ... >>> total_income 14100.0![]() 字典推導(dǎo)式,是一個和列表推導(dǎo)式一樣,具有很強(qiáng)大功能的知識點。因此,大家一定要掌握好。 例如,假設(shè)您有兩個數(shù)據(jù)列表,您需要根據(jù)它們創(chuàng)建一個新字典。 代碼語言:javascript復(fù)制>>> objects = ['blue', 'apple', 'dog'] >>> categories = ['color', 'fruit', 'pet'] >>> a_dict = {key: value for key, value in zip(categories, objects)} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}![]() 你會發(fā)現(xiàn),使用字典推導(dǎo)式,是一個更簡單、高效的作。 代碼語言:javascript復(fù)制>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {value: key for key, value in a_dict.items()} >>> new_dict {1: 'one', 2: 'two', 3: 'thee', 4: 'four'}![]() ![]() ![]() 從 Python 3.6 開始,字典是有序的數(shù)據(jù)結(jié)構(gòu),因此如果您使用 Python 3.6(及更高版本),您將能夠通過使用sorted()并借助字典理解對任何字典的鍵,進(jìn)行排序。 代碼語言:javascript復(fù)制>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> sorted_income = {k: incomes[k] for k in sorted(incomes)} >>> sorted_income {'apple': 5600.0, 'banana': 5000.0, 'orange': 3500.0}![]() Python 提供了一些內(nèi)置函數(shù),這些函數(shù)在您處理集合(如字典)時可能會很有用。 1.map()函數(shù) 假設(shè)您有一個包含一堆產(chǎn)品價格的字典,并且您需要對它們應(yīng)用折扣。 代碼語言:javascript復(fù)制>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25} >>> def discount(current_price): ... return (current_price[0], round(current_price[1] * 0.95, 2)) ... >>> new_prices = dict(map(discount, prices.items())) >>> new_prices {'apple': 0.38, 'orange': 0.33, 'banana': 0.24}![]() 2.filter()函數(shù) 假設(shè)您想知道單價低于0.40的產(chǎn)品。 代碼語言:javascript復(fù)制>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25} >>> def has_low_price(price): ... return prices[price] < 0.4 ... >>> low_price = list(filter(has_low_price, prices.keys())) >>> low_price ['orange', 'banana']![]() 這是很多人不清楚的概念,Python 3.5 帶來了一個有趣的新特性,因此大家需要著重學(xué)習(xí)。 您可以使用字典解釋運算符 ( **) 將兩個字典合并為一個新字典。 代碼語言:javascript復(fù)制>>> vegetable_prices = {'pepper': 0.20, 'onion': 0.55} >>> fruit_prices = {'apple': 0.40, 'orange': 0.35, 'pepper': .25} >>> {**vegetable_prices, **fruit_prices} {'pepper': 0.25, 'onion': 0.55, 'apple': 0.4, 'orange': 0.35}![]() 如果您嘗試合并的字典,具有重復(fù)或公共鍵,則最右側(cè)字典的值將補(bǔ)充上。 好啦,今天的分享就到這里啦,拜拜咯。 |
今日新聞 |
推薦新聞 |
專題文章 |
CopyRight 2018-2019 實驗室設(shè)備網(wǎng) 版權(quán)所有 |