11.3 字典复习
Posted jack20181017
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了11.3 字典复习相关的知识,希望对你有一定的参考价值。
dic2 = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}
# for k in dic2.keys():
# print(k)
#
# for k in dic2.values():
# print(k)
# for k,w in dic2.items():
# print(k,w) # 循环输出所有的k 和 v
# d 请在字典中添加一个键值对,"k4": "v4",输出添加后的字典
# dic2["k4"] = "v4"
# print(dic2)
# dic2.setdefault("k5",‘v5‘)
# print(dic2)
# e. 请在修改字典中 "k1" 对应的值为 "alex",输出修改后的字典
# dic2["k1"]= "alex"
# print(dic2)
# dic3={"k1":"balex"}
# dic2.update(dic3)
# print(dic2)
# f 请在k3对应的值中追加一个元素 44,输出修改后的字典
# dic2["k3"].append(44)
# print(dic2)
# dic2["k3"]=[11,22,33,55]
# print(dic2)
# g 请在k3对应的值的第 1 个位置插入个元素 18,输出修改后的字典
# dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}
# # dic2[‘k3‘]= [18,11,22,33]
# # print(dic2)
# dic2["k3"].insert(0,19) # 字典的插入.insert
# print(dic2)
# # 4、处理成字典 {‘k‘:1,‘k1‘:2....}
# st = "k:1|k1:2|k2:3|k3:4"
# dic={}
# lis = st.split("|")
# # print(lis) # [‘k:1‘, ‘k1:2‘, ‘k2:3‘, ‘k3:4‘]
# for d in lis:
# el = d.split(":")
# # print(el) # [‘k‘, ‘1‘]
# dic[el[0]]= int(el[1])
# print(dic)
以上是关于11.3 字典复习的主要内容,如果未能解决你的问题,请参考以下文章