python3筆記-字典
阿新 • • 發佈:2018-02-27
items 創建 value phone val spa exist IT OS
1 # 創建字典 2 d=dict(name=‘lily‘,age=18,phone=‘188011514000‘) 3 print(d) 4 # {‘name‘: ‘lily‘, ‘age‘: 18, ‘phone‘: ‘188011514000‘} 5 6 # 通過key獲取對應vlaue 7 name =d[‘name‘] 8 print(name) 9 # lily 10 11 # in,not in 判斷key是否存在 12 if ‘name‘ in d: 13 print(‘name is exist‘) 14 else: 15 print(‘no‘)16 17 # 獲取字典所有的key 18 keys = d.keys() 19 print(keys) 20 # dict_keys([‘name‘, ‘age‘, ‘phone‘]) 21 22 # dict.values() 返回一個包含字典所有value的列表 23 vlaue = d.values() 24 print(vlaue) 25 # dict_values([‘lily‘, 18, ‘188011514000‘]) 26 27 # dict.items()返回一個包含所有(鍵,值)元組的列表 28 keyandvlaue = d.items() 29 print(keyandvlaue)30 # dict_items([(‘name‘, ‘lily‘), (‘age‘, 18), (‘phone‘, ‘188011514000‘)]) 31 32 # dict.fromkeys(seq, val=None) 創建並返回一個新字典,以seq中的元素做該字典的鍵,val做該字典中所有鍵對應的初始值(默認為None) 33 lk =[‘xiao‘,‘da‘,‘hong‘] 34 dic = dict.fromkeys(lk) 35 print(dic) 36 # {‘xiao‘: None, ‘da‘: None, ‘hong‘: None} 37 38 39 a = lambda x:x+1 40print (a(3))
python3筆記-字典