4道Python基础字典练习题
1.写代码,有如下字典,按照要求实现每一个功能,dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
- 请循环输出所有的 key
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
for k in dic.keys():
print(k)
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
for k in dic:
print(k)
- 请循环输出所有的 value
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
for v in dic.values():
print(v)
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
for k in dic:
print(dic[k])
- 请循环输出所有的 key 和 value
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
for k in dic.keys():
print(k,dic[k])
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
for k in dic:
print(k,dic[k])
- 请在字典中添加一个键值对,‘k4’:‘v4’,输出添加后的字典
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic['k4'] = 'v4' #通过索引添加
print(dic)
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic.update({'k4':'v4'}) # 传一个字典
print(dic)
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic.update(k4 = "v4") # 传关键字
print(dic)
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic.update(zip(['k4'],['v4'])) # 传一个zip函数
print(dic)
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic.update([('k4','v4')]) # 传一个包含一个或多个元祖的列表
print(dic)
- 请在修改字典中“k1”对应的值为“alex”,输出修改后的字典
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic['k1'] = "alex"
print(dic)
- 请在k3对应的值中追加一个元素44,输出修改后的字典
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic['k3'].append("44")
print(dic
- 请在k3对应的值的第1个位置插入个元素18,输出修改后的字典
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic['k3'].insert(0,18)
print(dic)
- 请删除字典中键值对,‘k1’:‘v1’,并输出删除后的字典
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
dic.pop('k1')
print(dic)
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
del dic['k1']
print(dic)
- 请删除字典中的键’k5’对应的键值对,如果字典中不存在键’k5’,则不报错,并且让其返回 None
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
pop_k5 = dic.pop('k5',"None")
print(pop_k5)
- 请获取字典中’k2’对应的值
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
view_k2 = dic['k2']
print(view_k2)
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
view_k2 = dic.get('k2')
print(view_k2)
- 请获取字典中’k6’对应的值,如果键’k6’不存在,则不报错,并且让其返回 None
dic = {'k1':'v1','k2':'v2','k3':[11,22,33]}
view_k6 = dic.get('k6')
print(view_k6)
2.现有 dic2 = {‘k1’:‘v111’,‘a’:‘b’}通过一行操作使 dic2 = {‘k1’:‘v1’,‘k2’:‘v2’,‘k3’:‘v3’,‘a’,‘b’}
dic2 = {'k1':'v111','a':'b'}
dic2.update({'k1':'v1','k2':'v2','k3':'v3'})
print(dic2)
3.组合嵌套题。写代码,有如下列表,按照要求实现每一个功能
lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
(1).将列表 lis 中的’tt’变成大写(用两种方式)
lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
view_list_tt = lis[0][1][2]['k1']
view_tt = lis[0][1][2]['k1'][0]
view_list_tt[0] = view_tt.upper()
print(lis)
lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
view_list_tt = lis[0][1][2]['k1']
view_tt = lis[0][1][2]['k1'][0]
view_list_tt[0] = view_tt.swapcase()
print(lis)
(2).将列表中的数字 3 变成 字符串 ‘100’(用两种方式)
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
view_list_3 = lis[0][1][2]['k1']
view_3 = lis[0][1][2]['k1'][1]
view_list_3[1] = '100'
print(lis)
lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
lis[0][1][2].update({'k1': ['tt', 100, '1']})
print(lis)
(3).将列表中的字符串’1’变成数字 101 (用两种方式)
lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
view_list_1 = lis[0][1][2]['k1']
view_1 = lis[0][1][2]['k1'][2]
view_list_1[2] = 101
print(lis)
lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
lis[0][1][2].update({'k1': ['tt', 3, 101]})
print(lis)
4.按照要求实现以下功能
现有一个列表 li = [1,2,3,‘a’,‘b’,4,‘c’],有一个字典(此字典是动态生成的,你并不知道他里面有多少键值对,所以用 dic = {}模拟此字典),现在需要完成这样的操作:
如果该字典没有’k1’这个键,那就创建这个’k1’键和其对应的值(该键对应的值设置为空列表),并将列表 li 中的索引位为奇数对应的元素,添加到’k1’这个键对应的空列表中。
如果该字典中有’k1’这个键,且k1对应的value是列表类型,那就将列表 li 中的索引位为偶数对应的元素,添加到’k1’这个键对应的值中。
li = [1, 2, 3, 'a', 'b', 4, 'c']
dic = {} # 没有k1的情况
# dic = {'k1':[]} # 有k1的情况
# dic = {'k1':['e','f']} # 有k1的情况
if 'k1' not in dic:
li2 = []
dic.setdefault('k1', li2)
for i in li:
if li.index(i) % 2 == 1:
li2.append(i)
else:
if type(dic['k1']) == list:
li2 = dic['k1']
for i in li:
if li.index(i) % 2 == 0:
li2.append(i)
print(dic)