@dict的setdefault方法 先看看文档中的解释 setdefault(...) D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D 如果k不在字典中,就将k作为键值添加到字典D中,并且value值为d --------------------------------------------------------------------------- D.setdefault(k, []).append…
#!/usr/bin/env python #-*- coding:utf-8 -*- #打印0001-9999的数字 for i in range(9999): s = "%04d" % i print(s) #循环打印字典 dict1 = {"tom":90,"dgg":100,"sb":-1} for a in dict1: print(a,dict1[a]) #打印dict1的值 for value in dict1.…
前置知识 for 循环详解:https://www.cnblogs.com/poloyy/p/15087053.html 使用 for key in dict 遍历字典 可以使用 for key in dict 遍历字典中所有的键 x = {'a': 'A', 'b': 'B'} for key in x: print(key) # 输出结果 a b 使用 for key in dict.keys () 遍历字典的键 字典提供了 keys () 方法返回字典中所有的键 # keys book =…