筆記九 python之字典

2021-08-22 16:21:46 字數 4329 閱讀 5215

dict1 = 

dict1['phone'] = 12345678912

print(dict1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

process finished with exit code 0

dict1 = 

dict1['age'] = 21

print(dict1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

process finished with exit code 0

dict1 = 

result = dict1['age']

print(result)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

20process finished with exit code 0

dict1 = 

result = dict1.get('name')

print(result)

result1 = dict1.get('phone', '您輸入的索引不在字典中!')

print(result1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

張三您輸入的索引不在字典中!

process finished with exit code 0

dict1 = 

for key in dict1:

print('key:{} value:{}'.format(key, dict1[key]))

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

key:name value:張三

key:*** value:女

key:age value:20

process finished with exit code 0

dict1 = 

for key, values in dict1.items():

print('key:{} values:{}'.format(key, values))

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

key:name values:張三

key:*** values:女

key:age values:20

process finished with exit code 0

dict1 = 

del dict1['age']

print(dict1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

process finished with exit code 0

dict1 = 

dict1.pop('***')

print(dict1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

process finished with exit code 0

dict1 = 

result = dict1.popitem()

print(dict1)

print(result)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

('age', 20)

process finished with exit code 0

dict1 = 

dict1.clear()

print(dict1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

{}process finished with exit code 0

dict1 = 

result = dict1.keys()

print(result)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

dict_keys(['name', '***', 'age'])

process finished with exit code 0

dict1 = 

result = dict1.values()

print(result)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

dict_values(['張三', '女', 20])

process finished with exit code 0

dict1 = 

dict2 = dict1.copy()

print(dict2)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

process finished with exit code 0

dict1 = 

dict2 =

dict1.update(dict2)

print(dict1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

process finished with exit code 0

dict1 = 

dict1.setdefault('phone', '12345678912')

print(dict1)

dict1.setdefault('婚否')

print(dict1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

process finished with exit code 0

keys = ('name', '***', 'age')

dict1 = dict.fromkeys(keys, 100)

print(dict1)

dict1 = dict1.fromkeys(keys)

print(dict1)

d:\python\python37\python.exe c:/users/administrator/desktop/小嫻python/整理/整理csdn/筆記四.py

process finished with exit code 0

Python筆記之字典

range start,end step 2.建立字典的語法 dictionary 通過對映函式建立 dictionary dictionary zip list1,list2 通過給定的 關鍵字引數 建立字典 dictionary dict key1 value1,key2 value2,keyn...

Python學習筆記之字典

字典 dict 是python中的基本資料型別之一,是可變的無序集合,以鍵值對為基本元素的可以儲存各種資料型別的集合,用 表示字典的開始和結束,元素之間用,逗號分隔,和json資料類似。字典中的元素由鍵 key 和值 value 組成,中間用冒號做分隔。注意,字典中的鍵是不可變型別。以下是乙個典型的...

Python學習筆記之字典

一 建立和使用字典 1 建立字典 phonebook 2 dict,通過對映建立字典 items name gumby age 34 d dict items d 顯示 dict,通過關鍵字建立字典 d dict name gumby age 34 d 顯示 3 字典格式化字串 phonebook ...