#字典基本結(jié)構(gòu)
#字典名 = {鍵值對甲馋,鍵值對}鍵與值之間用’:‘隔開既忆,鍵值對之間用’,‘(逗號)隔開
#dictionary = {’color‘:‘green’作箍,’point‘:5}
#輸出格式? 字典名[鍵名(列表名)]
dictionary = {'color':'green','point':5}
print(dictionary['point'])
#字典中添加鍵值對
#直接看例子吧? ? ? ? 也就是直接給鍵值對賦值
#另外python中鍵值對的順序與添加順序無關,下方的xxx和yyy也有可能位置相換
print(dictionary)#輸出字典所有內(nèi)容
dictionary['xxx'] = 0
dictionary['yyy'] = 9
print(dictionary)
#修改字典中鍵值對中的值
dictionary['xxx'] = 100
print(dictionary['xxx'])
print(dictionary)
#刪除鍵值對? ? ? del刪除語句
del dictionary['yyy']
print(dictionary)
#創(chuàng)建多個相似對象的字典
study = {
? ? 'fan' : 'java',
? ? 'ren' : 'C++',
? ? 'chen' : 'python',
? ? }
print(study)
print('\n'+str(1))#換行符的簡單應用\n
#遍歷字典
#使用方法? for循環(huán)遍歷往毡,字典名.items()方法
#items()方法用來獲取字典內(nèi)所有鍵值對,所以for循環(huán)變量需要2個變量
# 返回順序和存儲順序不同蒙揣,python也不關心這個
for name,language in study.items():
? ? print('\n我的名字是'+name)
? ? print('我學習的語言是'+language)
#只遍歷字典中的鍵? ? ? 字典名.keys()和for循環(huán)實現(xiàn)
#? (了解即可) *for循環(huán)遍歷時默認遍歷鍵,所以keys()可以省略
for name in study.keys():
? ? print('name-----'+name)