Python基础(字典)
#创造和访问字典 ''' 字典是映射类型,元组和列表是序列类型 语法:字典名={关键字:值} ''' brand={"资产类别":"华为类别","品牌":"空","报废方式":"借","部门管理":"无"} print("资产类别",brand["资产类别"])#资产类别 华为类别 #更换value brand["资产类别"]="难搞" print(brand)#{'部门管理': '无', '资产类别': '难搞', '报废方式': '借', '品牌': '空'} brand=((("2","3"),("4",5)))#将元组作为参数 print(brand)#(('2', '3'), ('4', 5)) #字典的内嵌方法 ''' fromkeys(键值,值) ''' zidian={} print(zidian.fromkeys((1,2,3),"number"))#{1: 'number', 2: 'number', 3: 'number'} print(zidian.fromkeys((1,2,3),("number","two")))#{1: ('number', 'two'), 2: ('number', 'two'), 3: ('number', 'two')} print(zidian.fromkeys((1,2,3)))#{1: None, 2: None, 3: None} brand={"资产类别":"华为类别","品牌":"空","报废方式":"借","部门管理":"无"} #循环打印纸值 for eachvalue in brand.values(): print(eachvalue) #循环打印keys for eachkeys in brand.keys(): print(eachkeys) #循环打印keys与values for eachitem in brand.items(): print(eachitem) print(brand.get("他","无此项"))#无此项 ''' "他"in brand 如果这个字典中有他就会返回True,没有则返回false, brand.clear(),清空字典,copy()浅拷贝 '''