c语言sscanf函数的用法是什么
285
2022-11-26
36 字典中的items方法和key pop方法popitem方法setdefault update
第十课 获取字典中的key和value(items方法和key方法) # items方法和keys方法 # items 方法用于返回字典中所有的key value 对 获得的每一个kv对用一个元组来表示 items方法返回的值被成为字典视图的一个特殊类型 可以直接用于迭代 for 循环里面 # items 的返回值 与字典使用的同样的值 我们修改了一方的值,另外一方也会同样改变。 ''' key 方法用于返回字典中所有的key 与items方法类似,也可以用于迭代 for循环 ''' d = {'help':'帮助','bike':'自行车','plane':'飞机','China':'中国'} print(d.items()) # dict_items([('help', '帮助'), ('bike', '自行车'), ('plane', '飞机'), ('China', '中国')]) 输出的每一个元素就是一个元组 for key_value in d.items(): print("key","=",key_value[0],"value","=",key_value[1]) ''' key = help value = 帮助 key = bike value = 自行车 key = plane value = 飞机 key = China value = 中国 ''' for key,value in d.items(): print("{} = {}".format(key,value)) """ help = 帮助 bike = 自行车 plane = 飞机 China = 中国 """ print(('bike','自行车') in d.items()) # True dict_items = d.items() d['bike'] = '自行车;摩托车;电动自行车' print(dict_items) # dict_items([('help', '帮助'), ('bike', '自行车;摩托车;电动自行车'), ('plane', '飞机'), ('China', '中国')]) print(d.keys()) # dict_keys(['help', 'bike', 'plane', 'China']) for key in d.keys(): print(key) ''' help bike plane China ''' ---------------------------------------------------- 第十一课 弹出字典中的值(pop方法和popitem方法) # pop方法和popitem方法 # pop方法,用于获取指定的key的值,并从字典中弹出这个key-value对 # popitem方法用于返回字典中最后一个key-value对,并弹出key-value对 d = {'c':10,'a':40,'b':12,'x':45} d['1'] = 20 # 首先先添加2组 元素 d['5'] = 100 print(d) # {'c': 10, 'a': 40, 'b': 12, 'x': 45, '1': 20, '5': 100} print(d.pop('a')) # 40 获取指定key的值 print(d) #{'c': 10, 'b': 12, 'x': 45, '1': 20, '5': 100} a的这个值的kv元素一起弹出来了 #40 #{'c': 10, 'b': 12, 'x': 45, '1': 20, '5': 100} print(d.popitem()) # ('5', 100) for i in range(len(d)): print(d.popitem()) ''' 输出结果为: ('5', 100) ('1', 20) ('x', 45) ('b', 12) ('c', 10) ''' ---------------------------------------------------- 第十二课 添加但不修改value(setdefault方法) # setdefault方法: 如果这个key在字典中不存在,那么就在字典中添加一组值,然后返回value;如果这个key在字典中已经存在了,那么就会忽略后面的值,返回在字典中存在的值。如果我们不指定第二个参数值,那么就会返回None d = {"name":"Bill", "age":30} d['salary'] = 2000 d['age'] = 50 # 如何这个元素在字典中不存在,那么在后面会添加一组,如果存在那么久修改他的value print(d) #{'name': 'Bill', 'age': 50, 'salary': 2000} # None print(d.setdefault("location","中国")) print(d) ''' 中国 {'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中国'} ''' print(d.setdefault("location", "德国")) print(d) ''' 中国 {'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中国'} ''' print(d.setdefault("abc")) print(d) ''' None {'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中国', 'abc': None} ''' ''' 中国 {'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中国'} 中国 {'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中国'} None {'name': 'Bill', 'age': 50, 'salary': 2000, 'location': '中国', 'abc': None} ''' ---------------------------------------- 第十三课 成批导入字典数据(update方法) # update方法 # 用一个字典中的key-value对更新另外一个字典,该方法接收一个参数 # 用作数据源的字典 # dict1.update(dict2) 没有就插入,有的话,就更新 d1 = { 'title':'欧瑞学院', 'website':'https://geekori.com', 'description':'从事在线IT课程研发和销售' } d2 = { 'title':'欧瑞科技', 'products':['欧瑞学院','博客','读书频道','极客题库','OriUnity'], 'description':'从事在线IT课程研发和销售,工具软件研发' } d1.update(d2) # 用d2的字典去改变 d1 没有就直接增加,有的话,就用d2中的字典的值去更新update print(d1) ''' {'title': '欧瑞科技', 'website': ''description': '从事在线IT课程研发和销售,工具软件研发', 'products': ['欧瑞学院', '博客', '读书频道', '极客题库', 'OriUnity']} ''' ------------------------------------------------ 第十四课 获取字典中值的列表(values方法) # values方法: 获取字典中值的列表,value是可以重复的,可以有多个一样的 # keys方法: 用于返回字典中key的列表 key是唯一的,不可以重复的。 # items方法: 想得到字典中所有的key 和 value ; 要结合for 循环 d = { 'a':1, 'b':2, 'c':2, 'd':3, 'e':4, 'e':40 } print(d) # {'a': 1, 'b': 2, 'c': 2, 'd': 3, 'e': 40} 这里面e 的值被后面的覆盖了 print(d.values()) # dict_values([1, 2, 2, 3, 40]) print(d.keys()) # dict_keys(['a', 'b', 'c', 'd', 'e']) for value in d.values(): print(value) ''' 1 2 2 3 40 ''' # keys(只获取key)、values(只获取value)、items(获取key 和value)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~