python基础-6 字典相关练习题

写代码:有如下字典
按照要求实现每一个功能
dict = {"k1":"v1","k2":"v2","k3":"v3"}
1、请循环遍历出所有的key
1 dict = {"k1":"v1","k2":"v2","k3":"v3"}
2 
3 #1、请循环遍历出所有的key
4 for k in dict:
5     print(k)
View Code

2、请循环遍历出所有的value
1 for k in dict:
2     print(dict[k])
View Code

3、请循环遍历出所有的key,value
1 for k in dict:
2     print(k,dict[k])
View Code

 

4、添加一个键值对,‘k6’,‘v6’输出添加后的字典
1 dict = {"k1":"v1","k2":"v2","k3":"v3"}
2 print("dict原来的值: ",dict)
3 dict["k4"] = "v4"
4 print("dict新增\"k4\":\"v4\"后的值: ",dict)
View Code

 

5、请删除字典中键值对"k1":"v1",并输出删除后的结果
1 print(dic.pop('k6',None))
View Code

 

6、删除字典中键"k5"对应的键值对,如果字典中不存在键"k5",则不报错,返回None
1 dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
2 print(dict)
3 print("删除不存在的k5,不报错,返回值: ",dict.pop("k5",None))
View Code

 

7、请获取字典中"k2"对应的值
1 dict = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
2 print(dict["k2"])
View Code

 

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"}
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)
View Code

 

10、
组合嵌套题。写代码,有如下列表,按照要求实现每一个功能
"""
lis = [["k",["qwe",20,{"k1":["tt",3,"1"]},89],"ab"]]
10.1、将列表中的数字变成字符串"100"(用两种方法)
10.2、将列表中的字符串"1"变成数字101(用两种方法)
"""
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
View Code

#--------------------------华丽的分割线   ----------------------------

11、现有一个列表li = [1,2,3,'a',4,'c'],有一个字典(此字典是动态生成的,你并不知道他里面有多少
键值对,所以用dic={}模拟字典;现在需要完成这样的操作:如果该字典没有"k1"这个键,那就创建
这个"k1"键和对应的值(该键对应的值为空列表),并将列表li中的索引位为奇数对应的元素,添加到
"k1"这个键对应的空列表中。如果该字典中有"k1"这个键,且k1对应的value是列表类型。那就将该列表li
中的索引位为奇数对应的元素,添加到"k1",这个键对应的值中。

 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)
View Code

 

这个最有一个题是挺有意思的,其他的大多是一些基本操作,下次复习时间周六上午

 



 
posted @ 2018-03-21 11:47  Chuan_Chen  阅读(833)  评论(0编辑  收藏  举报
#waifu-toggle { background-color: #fa0; border-radius: 5px; bottom: 66px; color: #fff; cursor: pointer; font-size: 12px; right: 0; margin-right: -100px; padding: 5px 2px 5px 5px; position: fixed; transition: margin-right 1s; width: 60px; writing-mode: vertical-lr; } #waifu-toggle.waifu-toggle-active { margin-right: -40px; } #waifu-toggle.waifu-toggle-active:hover { margin-right: -30px; } #waifu { bottom: -1000px; right: 0; line-height: 0; margin-bottom: -10px; position: fixed; transform: translateY(3px); transition: transform .3s ease-in-out, bottom 3s ease-in-out; z-index: 1; } #waifu:hover { transform: translateY(0); } #waifu-tips { animation: shake 50s ease-in-out 5s infinite; background-color: rgba(236, 217, 188, .5); border: 1px solid rgba(224, 186, 140, .62); border-radius: 12px; box-shadow: 0 3px 15px 2px rgba(191, 158, 118, .2); font-size: 14px; line-height: 24px; margin: -30px 20px; min-height: 70px; opacity: 0; overflow: hidden; padding: 5px 10px; position: absolute; text-overflow: ellipsis; transition: opacity 1s; width: 250px; word-break: break-all; } #waifu-tips.waifu-tips-active { opacity: 1; transition: opacity .2s; } #waifu-tips span { color: #0099cc; } #waifu #live2d { cursor: grab; height: 300px; position: relative; width: 300px; } #waifu #live2d:active { cursor: grabbing; } #waifu-tool { color: #aaa; opacity: 0; position: absolute; left: -10px; top: 70px; transition: opacity 1s; } #waifu:hover #waifu-tool { opacity: 1; } #waifu-tool span { color: #7b8c9d; cursor: pointer; display: block; line-height: 30px; text-align: center; transition: color .3s; } #waifu-tool span:hover { color: #0684bd; /* #34495e */ } @keyframes shake { 2% { transform: translate(.5px, -1.5px) rotate(-.5deg); } 4% { transform: translate(.5px, 1.5px) rotate(1.5deg); } 6% { transform: translate(1.5px, 1.5px) rotate(1.5deg); } 8% { transform: translate(2.5px, 1.5px) rotate(.5deg); } 10% { transform: translate(.5px, 2.5px) rotate(.5deg); } 12% { transform: translate(1.5px, 1.5px) rotate(.5deg); } 14% { transform: translate(.5px, .5px) rotate(.5deg); } 16% { transform: translate(-1.5px, -.5px) rotate(1.5deg); } 18% { transform: translate(.5px, .5px) rotate(1.5deg); } 20% { transform: translate(2.5px, 2.5px) rotate(1.5deg); } 22% { transform: translate(.5px, -1.5px) rotate(1.5deg); } 24% { transform: translate(-1.5px, 1.5px) rotate(-.5deg); } 26% { transform: translate(1.5px, .5px) rotate(1.5deg); } 28% { transform: translate(-.5px, -.5px) rotate(-.5deg); } 30% { transform: translate(1.5px, -.5px) rotate(-.5deg); } 32% { transform: translate(2.5px, -1.5px) rotate(1.5deg); } 34% { transform: translate(2.5px, 2.5px) rotate(-.5deg); } 36% { transform: translate(.5px, -1.5px) rotate(.5deg); } 38% { transform: translate(2.5px, -.5px) rotate(-.5deg); } 40% { transform: translate(-.5px, 2.5px) rotate(.5deg); } 42% { transform: translate(-1.5px, 2.5px) rotate(.5deg); } 44% { transform: translate(-1.5px, 1.5px) rotate(.5deg); } 46% { transform: translate(1.5px, -.5px) rotate(-.5deg); } 48% { transform: translate(2.5px, -.5px) rotate(.5deg); } 50% { transform: translate(-1.5px, 1.5px) rotate(.5deg); } 52% { transform: translate(-.5px, 1.5px) rotate(.5deg); } 54% { transform: translate(-1.5px, 1.5px) rotate(.5deg); } 56% { transform: translate(.5px, 2.5px) rotate(1.5deg); } 58% { transform: translate(2.5px, 2.5px) rotate(.5deg); } 60% { transform: translate(2.5px, -1.5px) rotate(1.5deg); } 62% { transform: translate(-1.5px, .5px) rotate(1.5deg); } 64% { transform: translate(-1.5px, 1.5px) rotate(1.5deg); } 66% { transform: translate(.5px, 2.5px) rotate(1.5deg); } 68% { transform: translate(2.5px, -1.5px) rotate(1.5deg); } 70% { transform: translate(2.5px, 2.5px) rotate(.5deg); } 72% { transform: translate(-.5px, -1.5px) rotate(1.5deg); } 74% { transform: translate(-1.5px, 2.5px) rotate(1.5deg); } 76% { transform: translate(-1.5px, 2.5px) rotate(1.5deg); } 78% { transform: translate(-1.5px, 2.5px) rotate(.5deg); } 80% { transform: translate(-1.5px, .5px) rotate(-.5deg); } 82% { transform: translate(-1.5px, .5px) rotate(-.5deg); } 84% { transform: translate(-.5px, .5px) rotate(1.5deg); } 86% { transform: translate(2.5px, 1.5px) rotate(.5deg); } 88% { transform: translate(-1.5px, .5px) rotate(1.5deg); } 90% { transform: translate(-1.5px, -.5px) rotate(-.5deg); } 92% { transform: translate(-1.5px, -1.5px) rotate(1.5deg); } 94% { transform: translate(.5px, .5px) rotate(-.5deg); } 96% { transform: translate(2.5px, -.5px) rotate(-.5deg); } 98% { transform: translate(-1.5px, -1.5px) rotate(-.5deg); } 0%, 100% { transform: translate(0, 0) rotate(0); } } © 2022 GitHub, Inc. Terms Privacy Securi