Python 學習筆記(十)Python集合(二),python學習筆記
集合常用的方法
add() 向集合中增加一個元素,如果集合中已經有了這個元素,那個這個方法就會失效
1 >>> help(set.add) 2 Help on method_descriptor: 3 4 add(...) 5 Add an element to a set. #向集合中添加一個元素 6 7 This has no effect if the element is already present. #集合中已經存在元素,則這個方式失效 8 9 >>> a ={"baidu","google"}10 >>> type(a)11 <type 'set'>12 >>> a.add("weibo") #向集合a中添加元素13 >>> a14 set(['baidu', 'weibo', 'google'])15 >>> id(a) #集合a在記憶體中的地址16 64656104L17 >>> a.add("ali") #向集合a中添加元素18 >>> a19 set(['baidu', 'weibo', 'google', 'ali'])20 >>> id(a) #集合a中的記憶體位址沒有發生改變,是原地修改,是可變集合21 64656104L22 >>> a.add("google") #如果增加的元素在集合中存在,則不會做任何操作23 >>> b ={} #建立一個空的集合24 >>> b.add("python")25 Traceback (most recent call last):26 File "<stdin>", line 1, in <module>27 AttributeError: 'dict' object has no attribute 'add' #報錯資訊為字典中沒有add函數28 >>> type(b) #b是一個字典29 <type 'dict'>30 >>> b =set() #建立一個空集合31 >>> type(b)32 <type 'set'>33 >>> b.add("python") #向b中添加一個元素34 >>> b35 set(['python'])36 >>> b.add([1,2,3]) #向b中添加一個列表,報錯列表是不可hash的,是可改變的37 Traceback (most recent call last):38 File "<stdin>", line 1, in <module>39 TypeError: unhashable type: 'list'40 >>> b.add((1,2,3)) #可以向集合中添加一個元素41 >>> b42 set(['python', (1, 2, 3)])43 >>>
update() 更新
1 >>> help(set.update) 2 Help on method_descriptor: 3 4 update(...) 5 Update a set with the union of itself and others. #更新一個集合,用這個集合本身和另外 參數裡面的內容轉換為集合 6 7 >>> a 8 set(['baidu', 'weibo', 'google', 'ali']) 9 >>> b10 set(['python', (1, 2, 3)])11 >>> a.update(b) #將集合b更新到a集合中12 >>> a13 set(['baidu', 'weibo', 'google', 'ali', 'python', (1, 2, 3)])14 >>> a.update("test") #將一個字串更新到集合a中15 >>> a16 set(['baidu', 'weibo', 's', 'google', 'e', 't', 'ali', 'python', (1, 2, 3)])17 >>>
pop() 從集合中隨機刪除一個元素,並且把這個元素作為傳回值,pop函數沒有參數,不能指定元素
1 >>> help(set.pop) 2 Help on method_descriptor: 3 4 pop(...) 5 Remove and return an arbitrary set element. #從集合中移除一個元素,並且把這個元素返回 6 Raises KeyError if the set is empty. #如果這個集合為空白,那麼會報錯keyError 7 8 >>> b 9 set(['python', (1, 2, 3)])10 >>> b.pop()11 'python'12 >>> b13 set([(1, 2, 3)])14 >>>
remove() 從集合中刪除指定的元素,刪除的元素必須是集合中的一員,如果不是,則會報錯KeyError
1 >>> help(set.remove) 2 Help on method_descriptor: 3 4 remove(...) 5 Remove an element from a set; it must be a member. #從集合中刪除指定的元素,刪除的元素必須是集合中的一員 6 7 If the element is not a member, raise a KeyError. #如果不是集合中的元素,則會報錯KeyError 8 9 >>> a10 set(['baidu', 'weibo', 's', 'google', 'e', 't', 'ali', 'python', (1, 2, 3)])11 >>> a.remove("s")12 >>> a13 set(['baidu', 'weibo', 'google', 'e', 't', 'ali', 'python', (1, 2, 3)])14 >>> a.remove("s")15 Traceback (most recent call last):16 File "<stdin>", line 1, in <module>17 KeyError: 's'18 >>>
discard() 從集合中刪除指定的元素,刪除的元素必須是集合中的一員,如果不是,則不作任何操作
與remove()類似,區別就是remove() 刪除不是集合中的元素,則會報錯。而discard()刪除不是集合中的元素,則不會報錯。
樣本:
1 >>> help(set.discard) 2 Help on method_descriptor: 3 4 discard(...) 5 Remove an element from a set if it is a member. 6 7 If the element is not a member, do nothing. 8 9 >>> a.discard("s")10 >>>
clear() 刪除集合中所有的元素
1 >>> help(set.clear) 2 Help on method_descriptor: 3 4 clear(...) 5 Remove all elements from this set. 6 7 >>> a.clear() 8 >>> a #集合為一個空集合 9 set([])10 >>>