标签:ems 元素 字符 皇马 michael erro 多级嵌套 file ast
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。
如果用dict实现,只需要一个“名字”-“成绩”的对照表,直接根据名字查找成绩,无论这个表有多大,查找速度都不会变慢。用Python写一个dict如下:
>>> stu = {‘Michael‘: 95, ‘Bob‘: 75, ‘Tracy‘: 85} #创建字典 >>> stu[‘Michael‘] 95
>>> info = { ... ‘stu1101‘: "TengLan Wu", ... ‘stu1102‘: "LongZe Luola", ... ‘stu1103‘: "XiaoZe Maliya", ... } #创建一个字典info >>> >>> info {‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1101‘: ‘TengLan Wu‘, ‘stu1102‘: ‘LongZe Luola‘} >>> info["stu1104"] = "苍井空" #增加一个,如果没有这个键就增加,否则就修改这个键 >>> info {‘stu1104‘: ‘苍井空‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1101‘: ‘TengLan Wu‘, ‘stu1102‘: ‘LongZe Luola‘}
>>> info.pop("stu1101") #字典的内置方法,删除键名为stu1101 ‘TengLan Wu‘ >>> info {‘stu1104‘: ‘苍井空‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1102‘: ‘LongZe Luola‘} >>> del info[‘stu1103‘] #python自带的删除方法,删除键名为stu1103 >>> info {‘stu1104‘: ‘苍井空‘, ‘stu1102‘: ‘LongZe Luola‘} >>> info.popitem() #随机删除一个 (‘stu1104‘, ‘苍井空‘) >>> info {‘stu1102‘: ‘LongZe Luola‘}
>>> ‘stu1102‘ in info #判断成员关系 True >>> info.get(‘stu1102‘) #查找方法,如果查找对象不存在,此方法不报错,没有返回结果 ‘LongZe Luola‘ >>> info[‘stu1105‘] #此方法查找不存在的对象会报错 Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: ‘stu1105‘
>>> info = { ... ‘stu1101‘: "TengLan Wu", ... ‘stu1102‘: "LongZe Luola", ... ‘stu1103‘: "XiaoZe Maliya", ... } >>> >>> >>> info.values() #获取值 dict_values([‘XiaoZe Maliya‘, ‘TengLan Wu‘, ‘LongZe Luola‘]) >>> info.keys() #获取键名 dict_keys([‘stu1103‘, ‘stu1101‘, ‘stu1102‘]) >>> info.items() #获取键值对,以list的方式返回 dict_items([(‘stu1103‘, ‘XiaoZe Maliya‘), (‘stu1101‘, ‘TengLan Wu‘), (‘stu1102‘, ‘LongZe Luola‘)])
>>> info.setdefault("stu1106","Alex") ‘Alex‘ >>> info {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1106‘: ‘Alex‘} >>> info.setdefault("stu1102","龙泽萝拉") #因为字典中已有stu1102,并且字典有自动去重功能 ‘LongZe Luola‘ >>> info {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1106‘: ‘Alex‘}
>>> info {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1106‘: ‘Alex‘} >>> b = {1:2,3:4, "stu1102":"龙泽萝拉"} >>> info.update(b) #将字典b加入到字典info中 >>> info {‘stu1102‘: ‘龙泽萝拉‘, 1: 2, 3: 4, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1106‘: ‘Alex‘}
#方法1,建议使用 for key in info: print(key,info[key]) #方法2 for k,v in info.items(): #会先把dict转成list,数据里大时莫用 print(k,v)
>>> football = { ... ‘英超‘:{ ... ‘曼联‘:{ ... ‘积分‘:60, ... ‘进球‘:45, ... ‘排名‘:1 ... }, ... ‘阿森纳‘:{ ... ‘积分‘:55, ... ‘进球‘:30, ... ‘排名‘:4 ... }, ... ‘切尔西‘:{ ... ‘积分‘:59, ... ‘进球‘:40, ... ‘排名‘:2 ... } ... }, ... ‘中超‘:{ ... ‘广州恒大‘:{ ... ‘积分‘:45, ... ‘进球‘:25, ... ‘排名‘:1 ... }, ... ‘上海上港‘:{ ... ‘积分‘:20, ... ‘进球‘:10, ... ‘排名‘:2 ... }, ... ‘江苏苏宁‘:{ ... ‘积分‘:30, ... ‘进球‘:40, ... ‘排名‘:3 ... } ... }, ... ‘西甲‘:{ ... ‘巴萨‘:{ ... ‘积分‘:60, ... ‘进球‘:55, ... ‘排名‘:1 ... }, ... ‘皇马‘:{ ... ‘积分‘:55, ... ‘进球‘:59, ... ‘排名‘:2 ... }, ... ‘马竞‘:{ ... ‘积分‘:55, ... ‘进球‘:58, ... ‘排名‘:3 ... } ... } ... } >>> >>> football {‘中超‘: {‘江苏苏宁‘: {‘排名‘: 3, ‘积分‘: 30, ‘进球‘: 40}, ‘广州恒大‘: {‘排名‘: 1, ‘积分‘: 45, ‘进球‘: 25}, ‘上海上港‘: {‘排名‘ : 2, ‘积分‘: 20, ‘进球‘: 10}}, ‘英超‘: {‘曼联‘: {‘排名‘: 1, ‘积分‘: 60, ‘进球‘: 45}, ‘阿森纳‘: {‘排名‘: 4, ‘积分‘: 55, ‘进球‘: 30}, ‘切尔西‘: {‘排名‘: 2, ‘积分‘: 59, ‘进球‘: 40}}, ‘西甲‘: {‘马竞‘: {‘排名‘: 3, ‘积分‘: 55, ‘进球‘: 58}, ‘巴萨‘: {‘排名‘: 1, ‘积分‘: 60, ‘进球‘: 55}, ‘皇马‘: {‘排名‘: 2, ‘积分‘: 55, ‘进球‘: 59}}}
>>> football[‘中超‘][‘广州恒大‘]
{‘排名‘: 1, ‘积分‘: 45, ‘进球‘: 25}
>>> football[‘中超‘][‘广州恒大‘][‘积分‘]
45
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
>>> set1 = set([1,3,4,5,99,54,22]) >>> set2 = set([3,22,5,6,7,10,4]) >>> set1 {1, 99, 3, 4, 5, 54, 22} >>> set2 {3, 4, 5, 6, 7, 10, 22} >>> >>> set3 = set("Hello") #创建一个唯一字符的集合 >>> set3 {‘e‘, ‘l‘, ‘H‘, ‘o‘}
>>> set1.add(20) >>> set1 {1, 99, 3, 4, 5, 20, 54, 22} >>> set2.update([10,37,42]) >>> set2 {3, 4, 5, 6, 7, 37, 10, 42, 22}
>>> set1.remove(1) #如果元素不存在,会报错 >>> set1 {99, 3, 4, 5, 54, 22} >>> set1.discard(100) #如果元素存在就删除,不存在则do nothing >>> set1 {99, 3, 4, 5, 54, 22} >>> set1.pop() #使用pop()随意删除一个 99 >>> set1 {3, 4, 5, 54, 22} >>> len(set1) #set 的长度 5
>>> set1 = set([1,3,4,5,99,54,22]) >>> set2 = set([3,22,5,6,7,10,4]) >>> set1 {1, 99, 3, 4, 5, 54, 22} >>> set2 {3, 4, 5, 6, 7, 10, 22} #包含set1和set2中的每一个元素,取并集(即A和B) >>> set1.union(set2) {1, 99, 3, 4, 5, 6, 7, 10, 54, 22} >>> set3 = set1 | set2 >>> set3 {1, 99, 3, 4, 5, 6, 7, 10, 54, 22} #包含set1和set2中相同的元素,取交集(即AB中都有的) >>> set1.intersection(set2) {3, 4, 5, 22} >>> set3 = set1 & set2 >>> set3 {3, 4, 5, 22} #包含set1中有但是set2中没有的元素,取差集(即A有B没有) >>> set1.difference(set2) {1, 99, 54} >>> set3 = set1 - set2 >>> set3 {1, 99, 54} #包含set1和set2中不重复的元素,取对称差集(即A有B没有,B有A没有) >>> set1.symmetric_difference(set2) {1, 6, 7, 99, 10, 54} >>> set3 = set1 ^ set2 >>> set3 {1, 6, 7, 99, 10, 54} #set4是否set5的子集 >>> set4 = set([1,2,3,4,5,6,7,8,9]) >>> set4 {1, 2, 3, 4, 5, 6, 7, 8, 9} >>> set5 = set([1,3,5,7,9]) >>> set5 {1, 3, 5, 9, 7} >>> set4.issubset(set5) False >>> set4 {1, 2, 3, 4, 5, 6, 7, 8, 9} >>> set6 = set4 <= set5 >>> set6 False #set4是否包含set5的全部元素 >>> set4.issuperset(set5) True >>> set4 {1, 2, 3, 4, 5, 6, 7, 8, 9} >>> set6 = set4 >= set5 >>> set6 True
>>> set7 = set([1,2]) >>> set7 {1, 2} >>> 1 in set7 #测试1是否是set7的成员 True >>> set7 = set([1,2,100]) >>> set7 {1, 2, 100} >>> 100 not in set7 #测试100是否不是set7的成员 False
标签:ems 元素 字符 皇马 michael erro 多级嵌套 file ast
原文地址:http://www.cnblogs.com/kirusx/p/6523657.html