字典定義了鍵和值之間一對一的關係,但它們是以無序的方式儲存的。
dictionary 不只是用於儲存字串。dictionary 的值可以是任意資料型別,包括字串、整數、物件,甚至其它的 dictionary。
在單個 dictionary 裡,dictionary 的值並不需要全都是同一資料型別,可以根據需要混用和匹配。
dict1=
students=[,
,]print
('顯示該資料結構型別'
,type
(dict1)
)dict1
demo_dict=
dict
(zip
('abc'
,'123'))
#配對組成字典
demo_dict
print
('常規查詢\n'
,students[0]
['id'])
print
('根據鍵查詢\n'
,students[0]
.get(
'id'
))
)#在最後面增加乙個字典物件
('新增乙個字典物件後\n'
,students)
students[0]
['school']=
'school1'
#在列表的第乙個字典中增加鍵值對
students[1]
['school']=
'school2'
students[2]
['school']=
'school3'
students[2]
['school']=
'school3'
('增加鍵值對後的字典\n'
,students)
del students[3]
print
('刪除列表中的乙個字典物件後\n'
,students)
students[0]
.pop(
'id'
)print
('刪除乙個鍵值對後\n'
,students)
dict1.cleat(
)#清除所有元素
for i in
range(0
,len
(students)):
students[i]
.pop(
'school'
)students
students[0]
.update(
)#在列表第乙個的第乙個字典元素中修改鍵值對
print
('更新後的字典\n'
,students)
print
("字典值轉list"
,list
(demo_dict)
.values())
print
("字典鍵轉list"
,list
(demo_dict)
.key(
))
python學習筆記 字典
方法一 dict1 dict2 dict1,dict2 方法二 從python 2.2 版本起,可以使用乙個工廠方法,傳入乙個元素是列表的元組作為引數 fdict dict x 1 y 2 fdict 方法三 從python 2.3 版本起,可以用乙個很方便的內建方法fromkeys 來建立乙個 預...
Python學習筆記 字典
1 字典的定義 dictionary 字典 是除列表以外python之中最靈活的資料型別 字典同樣可以用來儲存多個資料 通常用於儲存描述乙個物體的相關資訊 和列表的區別 列表是有序的物件集合 索引,即從0開始,依次遞增 字典是無序的物件集合 字典的定義用 字典使用 鍵值對 儲存資料,鍵值對之間使用逗...
python學習筆記 字典
python學習筆記 字典 方法一 dict1 dict2 dict1,dict2 方法二 從python 2.2 版本起,可以使用乙個工廠方法,傳入乙個元素是列表的元組作為引數 fdict dict x 1 y 2 fdict 方法三 從python 2.3 版本起,可以用乙個很方便的內建方法fr...