Python 学习笔记 -- 字典、集合
1 #Python内置了字典,类似于C/C++的STL(map),C#的Dictionary 2 #字典使用键与值得方式存储,具有极快的查找速度 3 #注意一点:字典的值可以重复,但键唯一 4 #简单的字典创建实例 5 #----------------------------字典创建---------------------------- 6 print("#----------------------------字典创建----------------------------") 7 #实例一: 8 scores = {"omega":98,"alpha":99,"beta":100} 9 print("alpha scores:",scores["alpha"]) 10 11 12 dict1 = {} 13 dict1 = dict1.fromkeys((1,2,3),("number")) #使用内建函数创造一个键1,2,3,值都为number的字典 14 print(dict1) 15 dict1 = dict1.fromkeys((1,2),("数字")) #这里需要记住,fromkeys 只是用来创建字典,并不能修改 16 print(dict1) 17 print("#----------------------------------------------------------------") 18 #---------------------------------------------------------------- 19 #----------------------------字典使用---------------------------- 20 print("#----------------------------字典使用----------------------------") 21 #字典的更新:为scores继续添加元素 22 #注意:当键存在时,为改变键对应得值,当键不存在时创建键,并赋值 23 scores["gamma"] = "88" 24 print("gamma scores:",scores["gamma"]) 25 26 #内置函数dict的用法 27 a = dict(((1,"one"),(2,"two"),(3,"three"))) 28 print("打印字典a:",a) 29 b = dict(一="one",二="two",三="three") 30 print("打印字典b:",b) 31 32 #删除字典元素 33 c = {1:"one",2:"two",3:"three"} 34 print("字典c:",c) 35 del c[1] #删除键1 36 print("字典c:",c) 37 c.clear() #删除字典中所有的元素 38 print("字典c:",c) 39 del c #删除字典 40 41 print("#----------------------------------------------------------------") 42 #---------------------------------------------------------------- 43 #----------------------------字典函数---------------------------- 44 print("#----------------------------字典函数----------------------------") 45 46 #copy函数实例:返回一个字典的潜复制 47 a = {1:"one",2:"two",3:"three"} 48 b = a 49 c = a.copy() 50 print("字典a:",a," id=",id(a)) #注意观察直接赋值和调用copy函数的内存地址区别 51 print("字典b: ",b," id=",id(b)) 52 print("字典c: ",c," id=",id(c)) 53 54 #get函数实例:返回指定键的值,如果键不在字典中,返回default的值 55 #get(key,default=None) 56 a = {1:"one",2:"two"} 57 print(a.get(3)) 58 print(a.get(3, "没有")) 59 60 #key in dict实例:如果键在字典dict里放回true,否则false 61 a = {1:"one",2:"two"} 62 print(1 in a) 63 print(3 in a) 64 print(1 not in a) 65 print(3 not in a) 66 67 #items实例:返回一个可迭代的元组数组 68 a = {1:"one",2:"two"} 69 for key,value in a.items(): 70 print("key:",key," value:",value) 71 72 #keys实例:返回一个可迭代的,所有键的数字 73 a = {1:"one",2:"two"} 74 for key in a.keys(): 75 print("key:",key," value:",a[key]) 76 77 #setdefault 实例:和get()类似,但是如果键不在字典中,将会添加键并将值设为default 78 #setdefault(key,default=None) 79 a = {1:"one",2:"two"} 80 print("键3的值为:",a.setdefault(3,"three")) 81 print("现在字典a:",a) 82 83 #update 实例:dict2的键和值更新到dict里 84 #update(dict2) 85 a = {1:"one",2:"two"} 86 b = {3:"three"} 87 a.update(b) 88 print("现在字典a:",a) 89 90 #values 实例:返回字典中所有的值 91 a = {1:"one",2:"two"} 92 for values in a.values(): 93 print(values, end=" ") 94 print() 95 96 #pop 实例:给点键key,删除键,并返回对应的值 97 a = {1:"one",2:"two"} 98 b = a.pop(1) 99 print("被删除的值为:",b," 现在字典a:",a) 100 101 #popitem() 实例:随机返回并删除字典中的一对键和值(一般删除末尾) 102 a = {1:"one",2:"two"} 103 b = a.popitem() 104 print(a) 105 print(b) 106 107 print("#----------------------------------------------------------------") 108 #---------------------------------------------------------------- 109 #------------------------------集合------------------------------ 110 print("#------------------------------集合------------------------------") 111 #set和dict类似,是一堆键的集合,但不存储value,在集合中没有重复的key 112 #定义 113 s1 = set([1,2,3,4]) 114 s2 = {1,2,3,4} 115 print(s1) 116 print(s2) 117 118 #操作 119 s1 = {1,2,3} 120 s2 = {2,3,4} 121 s3 = s1 & s2 #交集 122 s4 = s1 | s2 #并集 123 s1.add(4) #添加 124 s2.remove(2) #删除 125 126 print("s1:",s1) 127 print("s2:",s2) 128 print("s3:",s3) 129 print("s4:",s4) 130 print(2 in s1) 131 132 print("#----------------------------------------------------------------") 133 #----------------------------------------------------------------