Python 字典操作
# Dict #当值一样时,批量创建键 dic_1 = dict.fromkeys(['host1', 'host2', 'host3'], 'test') print(dic_1) #字典排序 dic_2 = {'c':555, 'b':333, 'a':111} sorted(dic_2) sorted((dic_2.values())) print(sorted(dic_2)) print(sorted((dic_2.values()))) #字典遍历 dic = {'name':'mu', 'age':'24'} for i in dic: print(i) for i in dic.values(): print(i) for i in dic: print(i,dic[i]) # 推荐用这种,速度快,直接打印 for i in dic.items(): print(i) for i, v in dic.items(): # 数据需要转换,用这种方式,耗内存,并且时间长 print(i, v)

浙公网安备 33010602011771号