目錄
- 前言
- 字典推導
- dict.setdefault
- 總結
前言
本文為《流暢的python》的3.2-3.3節的學習筆記。
字典推導
DIAL_CODES = [(86, 'China'),(91, 'India'),(1, 'United States'),(62, 'Indonesia'),(55, 'Brazil'),(92, 'Pakistan'),(880, 'Bangladesh'),(234, 'Nigeria'),(7, 'Russia'),(81, 'Japan'),
]country_code = {country: codefor code, country in DIAL_CODES}
print(country_code)
>>>
{'China': 86, 'India': 91, 'United States': 1, 'Indonesia': 62, 'Brazil': 55, 'Pakistan': 92, 'Bangladesh': 880, 'Nigeria': 234, 'Russia': 7, 'Japan': 81}
說明:字典推導和列表推導,生成器推導寫法類似,使用{}包含key和value
本例中,DIAL_CODES為列表,用for循環得到每個元素,其類型為元組,用元組拆包的方法得到區域碼和國家名。
在字典推導時,以國家名為key,區域碼為value。
country_code1 = {code: country.upper()for country, code in country_code.items()if code < 66}
print(country_code1)
>>>
{1: 'UNITED STATES', 62: 'INDONESIA', 55: 'BRAZIL', 7: 'RUSSIA'}
說明:以上例生成的字典為base進行拆包,并過濾區域碼小于66的地區,推導新字典。
dict.setdefault
import sys
import reWORD_RE = re.compile(r'\w+')def get():index = {}with open('D:\\new 1.txt', encoding='utf-8') as fp:for line_no, line in enumerate(fp, 1):#match: the match objectfor match in WORD_RE.finditer(line):word = match.group()column_no = match.start() + 1location = (line_no, column_no)#occurrences: a list to include the location(type: turple)#if key=word is not in dict, occurrences is set to nulloccurrences = index.get(word, [])#1st search key#add the new valueoccurrences.append(location)index[word] = occurrences#2nd search key#print(occurrences)for word in sorted(index, key=str.upper):print(word, index[word])passif __name__ == '__main__':get()#setdefault()
>>>
0 [(4, 26), (4, 31)]
1 [(21, 16)]
2 [(20, 13), (21, 13)]
4 [(20, 16), (24, 9)]
5 [(24, 12)]
abs [(12, 21)]
bool [(12, 16)]
class [(3, 1)]
def [(4, 5), (7, 5), (9, 5), (11, 5), (13, 5), (17, 5)]
from [(1, 1)]
hypot [(1, 18), (10, 16)]
import [(1, 11)]
math [(1, 6)]
other [(13, 23), (14, 22), (15, 22)]
print [(23, 1)]
r [(8, 25), (8, 29)]
return [(8, 9), (10, 9), (12, 9), (16, 9), (18, 9)]
scalar [(17, 23), (18, 32), (18, 49)]
self [(4, 18), (5, 9), (6, 9), (7, 18), (8, 36), (8, 44), (9, 17), (10, 22), (10, 30), (11, 18), (12, 25), (13, 17), (14, 13), (15, 13), (17, 17), (18, 23), (18, 40)]
v1 [(20, 1), (23, 7)]
v2 [(21, 1), (23, 12)]
Vector [(3, 7), (8, 17), (16, 16), (18, 16), (20, 6), (21, 6), (24, 2)]
x [(4, 24), (5, 14), (5, 18), (8, 41), (10, 27), (14, 9), (14, 18), (14, 28), (16, 23), (18, 28)]
y [(4, 29), (6, 14), (6, 18), (8, 49), (10, 35), (15, 9), (15, 18), (15, 28), (16, 26), (18, 45)]
__abs__ [(9, 9)]
__add__ [(13, 9)]
__bool__ [(11, 9)]
__init__ [(4, 9)]
__mul__ [(17, 9)]
__repr__ [(7, 9)]
說明:本例是查詢所有字母數字字符串在文件里出現的位置。
字典key對應的value以occurrences表示,類型為列表,列表里每個位置以location表示,類型為元組。
occurrences = index.get(word, [])
使用get方法查詢字典index里是否有key為word,如果沒有則以[]作為默認值,這是第一次以key查詢字典。
occurrences.append(location)
index[word] = occurrences
在occurrences得到新值后,重新對字典key和value賦值,這是第二次以key查詢字典
以上三行可以通過dict.setdefault一行完成。
def setdefault():index = {}with open('D:\\new 1.txt', encoding='utf-8') as fp:for line_no, line in enumerate(fp, 1):#match: the match objectfor match in WORD_RE.finditer(line):word = match.group()column_no = match.start() + 1location = (line_no, column_no)index.setdefault(word,[]).append(location)for word in sorted(index, key=str.upper):print(word, index[word])pass
總結
本小節主要講字典的相關內容,字典推導和setdefault方法的運用。