##
字典
這個概念,將會伴隨著你後面的學習和工作。 字典是否是無序的
>>> # 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 >>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
鍵值互換
>>> 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'}
依據某種條件,過濾字典
>>> 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 <= 2: ... new_dict[key] = value ... >>> new_dict {'one': 1, 'two': 2}
利用字典中的值,做一些计算
>>> 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
字典推导式
>>> 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'}
利用字典推导式,实现键值转换
>>> 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'}
利用字典推导式,过滤字典
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {k: v for k, v in a_dict.items() if v <= 2} >>> new_dict {'one': 1, 'two': 2}
利用字典推导式,做一些计算
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> total_income = sum([value for value in incomes.values()]) >>> total_income 14100.0
字典排序
>> 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}
内置函数,与字典配合使用
map()函数
>>> 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}
filter()函数
>>> 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']
字典解包运算符
>>> 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}
以上是技巧|11個好用到起飛的「Python字典」知識點!的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器