写代码:有如下字典
按照要求实现每一个功能
dict = {"k1":"v1","k2":"v2","k3":"v3"}
1、请循环遍历出所有的key
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 dict = {"k1":"v1","k2":"v2","k3":"v3"} 2 3 #1、请循环遍历出所有的key 4 for k in dict: 5 print(k)
2、请循环遍历出所有的value
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 for k in dict: 2 print(dict[k])
3、请循环遍历出所有的key,value
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 for k in dict: 2 print(k,dict[k])
4、添加一个键值对,‘k6’,‘v6’输出添加后的字典
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 dict = {"k1":"v1","k2":"v2","k3":"v3"} 2 print("dict原来的值: ",dict) 3 dict["k4"] = "v4" 4 print("dict新增\"k4\":\"v4\"后的值: ",dict)
5、请删除字典中键值对"k1":"v1",并输出删除后的结果
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 print(dic.pop('k6',None))
6、删除字典中键"k5"对应的键值对,如果字典中不存在键"k5",则不报错,返回None
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} 2 print(dict) 3 print("删除不存在的k5,不报错,返回值: ",dict.pop("k5",None))
7、请获取字典中"k2"对应的值
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} 2 print(dict["k2"])
8、请获取字典中"k6"对应的值,如果不存在,则不报错,并且让其返回None。
dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} print("dict值: ",dict) print(dict.get("k6",None))
9、现有dict2 = {"k1":"v11","a":"b"},通过一行操作使dict2 = {"k1":"v1","k2":"v2","k3":"v3","a":"b"}
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 dict = {"k1":"v1","k2":"v2","k3":"v3"} 2 dict2 = {"k1":"v11","a":"b"} 3 dict2.update(dict) 4 print("dict: ",dict) 5 print("dict2: ",dict2)
10、
组合嵌套题。写代码,有如下列表,按照要求实现每一个功能
"""
lis = [["k",["qwe",20,{"k1":["tt",3,"1"]},89],"ab"]]
10.1、将列表中的数字变成字符串"100"(用两种方法)
10.2、将列表中的字符串"1"变成数字101(用两种方法)
"""
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 # 10.1 方法1: 2 print(lis[0][1][2].get("k1")[0].upper()) 3 # 10.1 方法2: 4 print(lis[0][1][2].get("k1")[0].swapcase()) 5 6 # 10.2 方法1: 7 lis[0][1][2]["k1"][2] = 101 8 # 10.2 方法2: 9 lis[0][1][2].get("k1")[2]=101
#--------------------------华丽的分割线 ----------------------------
11、现有一个列表li = [1,2,3,'a',4,'c'],有一个字典(此字典是动态生成的,你并不知道他里面有多少
键值对,所以用dic={}模拟字典;现在需要完成这样的操作:如果该字典没有"k1"这个键,那就创建
这个"k1"键和对应的值(该键对应的值为空列表),并将列表li中的索引位为奇数对应的元素,添加到
"k1"这个键对应的空列表中。如果该字典中有"k1"这个键,且k1对应的value是列表类型。那就将该列表li
中的索引位为奇数对应的元素,添加到"k1",这个键对应的值中。
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 dic = {} 2 li = [1,2,3,'a',4,'c'] 3 if "k1" not in dic: 4 dic.setdefault("k1",[]) 5 for i in li: 6 if li.index(i) %2 == 1: 7 dic["ki"].append(i) 8 else: 9 if type(dic["k1"]) == type([]): 10 for i in li: 11 if li.index(i) %2 ==1: 12 dic["k1"].append(i) 13 else: 14 print('字典k1,value不是列表,无法追加') 15 16 print(li,dic)
这个最有一个题是挺有意思的,其他的大多是一些基本操作,下次复习时间周六上午