标签:int dict print python 内容 span 语法 nan 循环
语法如下:
1 dict1 = {‘name‘:‘huangmeiling‘,‘age‘:10,‘address‘:‘nanjing‘} 2 #print(dir(dict1)) 3 4 # 获取到所有的key值 5 #print(dict1.keys()) 6 #for key in dict1.keys(): 7 # print(key) 8 9 # 获取所有的value值 10 #for value in dict1.values(): 11 # print(value) 12 13 #对字典循环 14 #for key,value in dict1.items(): 15 # print(key,‘:‘,value) 16 17 #获取key对应的value值(建议使用第一种) 18 # print(dict1[‘age‘]) 19 # print(dict1.get(‘age‘)) 20 21 # 判断key是否存在(has_key在python2.2版本后不存在了) 22 ‘‘‘if dict1.has_key(‘age‘): 23 if age>5: 24 print(dict1[‘age‘]) 25 else: 26 print(‘sorry‘)‘‘‘ 27 28 # pop--删除指定的key对应的value值,并且把删除的内容打印出来 29 # print(dict1.pop(‘age‘)) 30 # print(dict1) 31 32 #clear-->全部清空 33 #dict1.clear() 34 #print(dict1) 35 36 # copy --->复制 37 dict2=dict1.copy() 38 print(dict2)
标签:int dict print python 内容 span 语法 nan 循环
原文地址:https://www.cnblogs.com/1510152012huang/p/10188815.html