Set and dictionary set (variable, unordered, non-repeating) initialization
#set()->new empty set ibjecta=set()print(a) #输出:set()#set(iterable)->new set objectprint(set(range(5))) #输出:{0, 1, 2, 3, 4}print(set(list(range(5)))) #输出:{0, 1, 2, 3, 4}s={} #1print(s)print(type(s)) #输出:<class ‘dict‘>s1={(1,2),3,‘a‘}print(s1) #输出:{(1, 2), ‘a‘, 3}s2={[1],(1,),1} #2print(s2) #TypeError: unhashable type: ‘list‘
1 When there is no value in {}, the system will be judged as a dictionary; 2set elements must be hash, that is, immutable;
Note: Set can iterate, but there is no index because he is unordered;
Set operation
#set(elem)增加s={1,}s.add(2)print(s) #输出:{1,2}s.add(1)print(s) #输出:{1,2}#1#update(*other)#合并其他元素到set中,other必须是可迭代对象s1={‘da‘,31,‘dd‘,234,‘f‘,25,}s2={312,"2",31,"312ds"}print(s1.update(s2)) #输出:Noneprint(s1) #输出:{‘f‘, ‘da‘, ‘dd‘, 234, ‘312ds‘, ‘2‘, 312, 25, 31}
1 If an element exists, nothing is done
#remove(elem)#从set中移除一个元素,不存在抛异常a={‘f‘, ‘da‘, ‘dd‘, 234, ‘312ds‘, ‘2‘, 312, 25, 31}print(a.remove(‘f‘)) #输出:Noneprint(a) #输出:{‘2‘, 234, ‘da‘, ‘312ds‘, 312, 25, ‘dd‘, 31}#discard(elem)#从set中移除一个元素,不存在什么也不做b={234, ‘312ds‘, ‘2‘, 312, 25, 31}print(b.discard(234)) #输出:Noneprint(b) #输出:{‘2‘, 312, 25, ‘312ds‘, 31}#1#pop()->item#移除并返回任意元素c={312, 25, 31,1789}print(c.pop())print(c)#clear()#移除所有元素d={312, 25, 31,1789}print(d.clear())print(d)
1 in contrast to remove and discard, priority should be given to using discard his error hints are milder;
Note: Set for non-linear unordered, all nature has not changed this said, can delete one, add one to simulate change
Dictionary (variable, unordered, can not be duplicated) initialization
#空字典d1={}d2=dict()print(d1)print(d2)#使用key=value来初始化d3=dict(a=12,b=3)print(d3) #输出:{‘a‘: 12, ‘b‘: 3}#以下几种方法,必须是二元可迭代结构才能d4=dict(((1,‘a‘),(2,‘b‘)))print(d4) #输出:{1: ‘a‘, 2: ‘b‘}d5=dict(([1,‘1‘],[2,‘2‘]))print(d5) #输出:{1: ‘1‘, 2: ‘2‘}d6=dict([[1,‘1‘],[2,‘2‘]])print(d6) #输出:{1: ‘1‘, 2: ‘2‘}#dict(mapping,**kwarg)使用一个字典构建领一个字典d7=dict(d6,a=2)print(d7)#最简单的定义方法d8={‘a‘:1,‘q‘:3}print(d8)#类方法dict.fromkeys(iterable,value),用来统一赋值d=dict.fromkeys(range(5))print(d)d=dict.fromkeys(range(5),10)print(d)
Access to dictionary elements
#d[key],不存在抛异常d={1: ‘1‘, 2: ‘2‘, ‘a‘: 2}print(d[‘a‘])#get(key,[,default]) 返回key对应的值value,后面可是设置缺省值,不存在返回Noned={1: ‘1‘, 2: ‘2‘, ‘a‘: 2}print(d.get(2,"exit")) #输出:2 print(d.get(23,"no exit")) #输出:no exitprint(d)#setdefault(key,[,default]) 返回key对应的值value,后面可是设置缺省值,不存在返回Noneprint(d.setdefault(23,"no exit"))print(d) #输出:{1: ‘1‘, 2: ‘2‘, ‘a‘: 2, 23: ‘no exit‘}#1
1: From the above it seems that if you see the dictionary there is no content and to join, then you need to use SetDefault
Additions and modifications to dictionaries
#d[key]=value,如果key不存在那么是添加,如果存在是修改d={1: ‘1‘, 2: ‘2‘, ‘a‘: 2}d["wuyanzu"]="123"print(d) #输出:{1: ‘1‘, 2: ‘2‘, ‘a‘: 2, ‘wuyanzu‘: ‘123‘}#update([other])->None#使用另一个字典的kv来更新本字典,key不存在就添加,存在就覆盖d1=dict(apple=‘a‘,pear=‘p‘)print(d1.update(d)) #输出:Noneprint(d1) #输出:{‘apple‘: ‘a‘, ‘pear‘: ‘p‘, 1: ‘1‘, 2: ‘2‘, ‘a‘: 2, ‘wuyanzu‘: ‘123‘}
Deletion of dictionaries
a={1: ‘1‘, 2: ‘2‘, ‘a‘: 2, ‘wuyanzu‘: ‘123‘}#pop(key,[,default])#key存在print(a.pop(1)) #输出:1,返回的是keyprint(a) #输出:{2: ‘2‘, ‘a‘: 2, ‘wuyanzu‘: ‘123‘}#key不存在,default存在a={1: ‘1‘, 2: ‘2‘, ‘a‘: 2, ‘wuyanzu‘: ‘123‘}print(a.pop(3,"no exist")) #输出:no exist#key不存在,default不存在a={1: ‘1‘, 2: ‘2‘, ‘a‘: 2, ‘wuyanzu‘: ‘123‘}print(a.pop(3)) #抛异常,keyerrora={1: ‘1‘, 2: ‘2‘, ‘a‘: 2, ‘wuyanzu‘: ‘123‘}#popitem移除并返回任意一个键值对,直到空字典时,抛出异常print(a.popitem())print(a.clear())print(a)#输出(‘wuyanzu‘, ‘123‘)None{}d={‘a‘:1,‘b‘:‘b‘,‘c‘:[1,3,5]}#deldel d[‘c‘]print(d)#输出:{‘a‘: 1, ‘b‘: ‘b‘}
Del Delete is to reduce the reference to the object, not to delete the contents of the reference in memory;
Dictionary traversal
d={‘a‘:1,‘b‘:‘b‘,‘c‘:[1,3,5]}#key遍历for k in d.keys(): print(k)#输出abcfor k in d: print(d[k]) #默认也是对key进行遍历;#value遍历for v in d.values(): print(v)#输出1b[1, 3, 5]#k,v都遍历for k,v in d.items(): print(k,v)#输出a 1b bc [1, 3, 5]
The dictionary key and set requirements are consistent, must be hashable (immutable type);
python-Collection, Dictionary