第七章 組合數(shù)據(jù)-4字典Dictionary
字典可存儲任意類型對象。字典的每個(gè)鍵值對 key=>value用冒號 : 分割,每個(gè)鍵值對之間用逗號 , 分割,整個(gè)字典包括在花括號 {} 中 ,舉例如下所示:
dict = {'Name': 'AJ', 'Age': 7, 'Class': 'Miss daisy'}
print(dict['Name'],dict['Age'],dict['Class'])???????????? #AJ 7 Miss daisy
print(dict)?????????????????????????????????????? #{'Name': 'AJ', 'Age': 7, 'Class': 'Miss daisy'}
dict['Age'] = 8????????? ?????????????????????????# 更新
dict['School'] = "CUP"???????????????????????????? # 添加
print(dict)???????????????????????? # {'Name': 'AJ', 'Age': 8, 'Class': 'Miss daisy', 'School': 'CUP'}
del dict['Name']????????????????? # 刪除鍵是'Name'的條目
dict.clear()???? ??????????????????# 清空字典所有條目
del dict???????????????????????? # 刪除字典
?


dict1 = {'user': 'CISE', 'num': [6, 1, 4]}
dict2 = dict1???????? # 直接賦值,dict2是dict1引用
dict3 = dict1.copy()?? # 淺拷貝
print(id(dict1),id(dict2),id(dict3))
# 修改 data 數(shù)據(jù)
dict1['user'] = 'CUP'
dict1['num'].remove(1)
# 輸出結(jié)果
print(dict1)
print(dict2)
print(dict3)
運(yùn)行結(jié)果如下:
{'user': 'CUP', 'num': [6, 4]}
{'user': 'CUP', 'num': [6, 4]}
{'user': 'CISE', 'num': [6, 4]}
實(shí)例中 dict2 其實(shí)是 dict1 的引用(別名),所以輸出結(jié)果都是一致的,dict3 父對象user-CISE進(jìn)行了深拷貝,不會隨dict1 修改而修改,子對象num-1是淺拷貝所以隨 dict1 的修改而修改。
?
seq = ('CUP', 'CISE', '614')
dict = dict.fromkeys(seq)
print("新字典1為 : %s" %? str(dict))
print("字典值 : %s" %? dict.items())
dict2 = {'Address': '府學(xué)路18號' }
dict.update(dict2)
print("字典值 : %s" %? dict.items())
dict = dict.fromkeys(seq, [1,2,3])
print("新字典2為 : %s" %? str(dict))
print("Value : %s" %? dict.get('CUP'))
dict.pop('CISE')
pop_obj=dict.popitem()
print(pop_obj)
print(dict)
運(yùn)行結(jié)果如下:
新字典1為 : {'CUP': None, 'CISE': None, '614': None}
字典值 : dict_items([('CUP', None), ('CISE', None), ('614', None)])
字典值 : dict_items([('CUP', None), ('CISE', None), ('614', None), ('Address', '府學(xué)路18號')])
新字典2為 : {'CUP': [1, 2, 3], 'CISE': [1, 2, 3], '614': [1, 2, 3]}
Value : [1, 2, 3]
('614', [1, 2, 3])
{'CUP': [1, 2, 3]}
?
參考例子:
stulist=[]
studisc1={"name":"wangyan","sex":"male","IQ":10}
stulist.append(studisc1)
studisc2={"name":"pp","sex":"female","IQ":300}
stulist.append(studisc2)
studisc3={"name":"aa","sex":"male","IQ":300}
stulist.append(studisc3)
studisc4={"name":"huishan","sex":"male","IQ":15}
stulist.append(studisc4)
findname=input("input name:")
find=False
for s in stulist:
??? if s["name"]==findname:
??????? find=True
??????? print("找到了! Info:")
??????? print(s)
??????? break
if not find:
??? print("sorry,沒找到!")
print("查找結(jié)束")
?
運(yùn)行結(jié)果:
input name:qq
sorry,沒找到!
查找結(jié)束
?
也可以使用下面方法:
stulist=[]
studisc1={"name":"wangyan","sex":"male","IQ":10}
stulist.append(studisc1)
studisc2={"name":"pp","sex":"female","IQ":300}
stulist.append(studisc2)
studisc3={"name":"aa","sex":"male","IQ":300}
stulist.append(studisc3)
studisc4={"name":"huishan","sex":"male","IQ":15}
stulist.append(studisc4)
findname=input("input name:")
find=False
for s in stulist:
??? if s["name"]==findname:
??????? find=True
??????? print("找到了! Info:")
??????? print(s)
??????? break
else:
??? print("sorry,沒找到!")
print("查找結(jié)束")
?
運(yùn)行結(jié)果:
input name:aa
找到了! Info:
{'name': 'aa', 'sex': 'male', 'IQ': 300}
查找結(jié)束
?
還可以使用字典加列表試一下名片管理系統(tǒng),有點(diǎn)不好說呢。