寫代碼:有以下字典
按照要求實現每個功能
dict = {"k1":"v1","k2":"v2","k3":"v3"}
一、請循環遍歷出全部的key
dict = {"k1":"v1","k2":"v2","k3":"v3"}
#一、請循環遍歷出全部的key for k in dict: print(k)
[root@webserver test]# cat loop_dict_keys.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#一、請循環遍歷出全部的key
dict = {"k1":"v1","k2":"v2","k3":"v3"}
for k in dict:
print(k)
[root@webserver test]# python3 loop_dict_keys.py
k1
k2
k3
[root@webserver test]# python
#二、請循環遍歷出全部的value for k in dict: print(dict[k])
[root@webserver test]# cat loop_dict_values.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#二、請循環遍歷出全部的value
dict = {"k1":"v1","k2":"v2","k3":"v3"}
for k in dict:
print(dict[k])
[root@webserver test]# python3 loop_dict_values.py
v1
v2
v3
[root@webserver test]# web
#三、請循環遍歷出全部的key和value for k in dict: print(k,dict[k])
[root@webserver test]# cat loop_dict_kv.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#一、請循環遍歷出全部的key
dict = {"k1":"v1","k2":"v2","k3":"v3"}
for k in dict:
print(k,dict[k])
[root@webserver test]# python3 loop_dict_kv.py
k1 v1
k2 v2
k3 v3
[root@webserver test]# app
#四、請在字典中增長一個鍵值對,"k4":"v4",輸出添加後的字典 dict = {"k1":"v1","k2":"v2","k3":"v3"} print("dict原來的值: ",dict) dict["k4"] = "v4" print("dict新增\"k4\":\"v4\"後的值: ",dict)
[root@webserver test]# cat insert_dict.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#四、請在字典中增長一個鍵值對,"k4":"v4",輸出添加後的字典
dict = {"k1":"v1","k2":"v2","k3":"v3"}
print("dict原來的值: ",dict)
dict["k4"] = "v4"
print("dict新增\"k4\":\"v4\"後的值: ",dict)
[root@webserver test]# python3 insert_dict.py
dict原來的值: {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}
dict新增"k4":"v4"後的值: {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}
[root@webserver test]# oop
#五、請刪除字典中鍵值對"k1":"v1",並輸出刪除後的結果 dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} print("dict刪除k1前的值: ",dict) dict.pop("k1") print("dict刪除k1後的值: ",dict)
[root@webserver test]# cat delete.dict.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#五、請刪除字典中鍵值對"k1":"v1",並輸出刪除後的結果
dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
print("dict刪除k1前的值: ",dict)
dict.pop("k1")
print("dict刪除k1後的值: ",dict)
[root@webserver test]# python3 delete.dict.py
dict刪除k1前的值: {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}
dict刪除k1後的值: {'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}
[root@webserver test]# spa
dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
print(dict)
print("刪除不存在的k5,不報錯,返回值: ",dict.pop("k5",None))code
[root@webserver test]# cat not_dict_k.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#六、請刪除字典中鍵"k5"對應的鍵值對,若是字典中不存在鍵"k5",則不報錯,返回None
dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
print(dict)
print("刪除不存在的k5,不報錯,返回值: ",dict.pop("k5",None))server
[root@webserver test]# python3 not_dict_k.py
{'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}
刪除不存在的k5,不報錯,返回值: None
[root@webserver test]#blog
# 七、請獲取字典中"k2"對應的值 dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} print(dict["k2"])
[root@webserver test]# cat get_exist.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# 七、請獲取字典中"k2"對應的值
dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
print(dict["k2"])
[root@webserver test]# python3 get_exist.py
v2
[root@webserver test]# 索引
#八、請獲取字典中"k6"對應的值,若是不存在,則不報錯,而且讓其返回None。 dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} print("dict值: ",dict) print(dict.get("k6",None))
#八、請獲取字典中"k6"對應的值,若是不存在,則不報錯,而且讓其返回None。
dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
print("dict值: ",dict)
print(dict.get("k6",None))
[root@webserver test]# python3 get_not_exist.py
dict值: {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}
None
[root@webserver test]# utf-8
#九、現有dict2 = {"k1":"v11","a":"b"},經過一行操做使dict2 = {"k1":"v1","k2":"v2","k3":"v3","a":"b"} dict = {"k1":"v1","k2":"v2","k3":"v3"} dict2 = {"k1":"v11","a":"b"} dict2.update(dict) print("dict: ",dict) print("dict2: ",dict2)
[root@webserver test]# cat update_dict_union.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#九、現有dict2 = {"k1":"v11","a":"b"},經過一行操做使dict2 = {"k1":"v1","k2":"v2","k3":"v3","a":"b"}
dict = {"k1":"v1","k2":"v2","k3":"v3"}
dict2 = {"k1":"v11","a":"b"}
dict2.update(dict)
print("dict: ",dict)
print("dict2: ",dict2)
[root@webserver test]# python3 update_dict_union.py
dict: {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}
dict2: {'k1': 'v1', 'a': 'b', 'k2': 'v2', 'k3': 'v3'}
[root@webserver test]#
"""
lis = [["k",["qwe",20,{"k1":["tt",3,"1"]},89],"ab"]]
10.一、將列表中的數字變成字符串"100"(用兩種方法)
10.二、將列表中的字符串"1"變成數字101(用兩種方法)
"""
# 10.1 方法1: print(lis[0][1][2].get("k1")[0].upper()) # 10.1 方法2: print(lis[0][1][2].get("k1")[0].swapcase()) # 10.2 方法1: lis[0][1][2]["k1"][2] = 101 # 10.2 方法2: lis[0][1][2].get("k1")[2]=101
十一、
""" 現有一個列表li = [1,2,3,'a',4,'c'],有一個字典(此字典是動態生成的,你並不知道他裏面有多少 鍵值對,因此用dic={}模擬字典;如今須要完成這樣的操做:若是該字典沒有"k1"這個鍵,那就建立 這個"k1"鍵和對應的值(該鍵對應的值爲空列表),並將列表li中的索引位爲奇數對應的元素,添加到 "k1"這個鍵對應的空列表中。若是該字典中有"k1"這個鍵,且k1對應的value是列表類型。那就將該列表li 中的索引位爲奇數對應的元素,添加到"k1",這個鍵對應的值中。 """ dic = {} li = [1,2,3,'a',4,'c'] if "k1" not in dic: dic.setdefault("k1",[]) for i in li: if li.index(i)%2 == 1: dic["k1"].append(i) else: if type(dic["k1"]) == type([]): for i in li: if li.index(i)%2 == 1: dic["k1"].append(i) else: print("字典K1,VALUE不是列表,沒法追加") print(dic)
[root@webserver test]# cat union_dic_lis.py #!/usr/bin/env python3# -*- coding:utf-8 -*-dic = {}li = [1,2,3,'a',4,'c']if "k1" not in dic: dic.setdefault("k1",[]) for i in li: if li.index(i)%2 == 1: dic["k1"].append(i) else: if type(dic["k1"]) == type([]): for i in li: if li.index(i)%2 == 1: dic["k1"].append(i) else: print("字典K1,VALUE不是列表,沒法追加")print(dic)[root@webserver test]# python3 union_dic_lis.py {'k1': [2, 'a', 'c', 2, 2, 'a', 'c', 'a', 2, 'a', 'c', 'c']}[root@webserver test]#