python學習筆記

johnchou發表於2021-09-09

python學習筆記(六)之集合1
python中各種型別與其各種方法,都可以使用下面的方法查到:
(1)互動模式下用dir()或者help()
(2)google
集合
特點:英語set,有的可變,有的不可變;元素無次序,不可重複。
集合沒有索引(可以使用dir(set)檢視),也就沒有順序而言,它不屬於序列。
集合中的元素是hashable(不可變)型別!

建立集合--字串
例項1:

>> s1 = set("wtf")
>> s1
set(['t', 'w', 'f'])
>> type(s1)

把字串的字元拆解開,形成集合。
例項2:
>> s2 = set("wttf")
>> s2
set(['t', 'w', 'f'])
>> type(s2)

說明:"wttf"中有兩個"t",但在集合中,只有一個"t",這也說明了集合中的元素是不能重複。

建立集合--列表
例項3:

>> s3 = set([123,"wtf","book","wtf"])
>> s3
set([123, 'book', 'wtf'])
說明:建立集合的時候,如果發現列表中有重複的元素,就會過濾掉,剩下不重複的。

add

>> help(set.add)
add(...)
Add an element to a set.
This has no effect if the element is already present.
例項4:
>> a_set = {}
>> a_set.add("wtf")
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'dict' object has no attribute 'add'
>> type(a_set)

說明:{}這個東西,在字典和集合中都用。但是,如上面的方法建立的是字典,不是集合。這是python的規定。
用()也不行,如下:
例項5:
>> a_set = ()
>> a_set.add("wtf")
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'tuple' object has no attribute 'add'
>> type(a_set)

說明:計算機認為建立的是元組。

要建立空集合,不得不使用set()
例項6:

>> s = set()
>> type(s)

要建立非空集合,如下:
例項7:

>> a_set = {"a","i"}
>> type(a_set)

>> print a_set
set(['i', 'a'])
或者:
>> a_set = set(["a","i"])
>> type(a_set)

>> print a_set
set(['a', 'i'])

增加元素:
例項8:

>> a_set.add("wtf")
>> a_set
set(['i', 'a', 'wtf'])

update
特點:將另外一個集合中元素合併過來。

>> help(set.update)
update(...)
Update a set with the union of itself and others.
例項9:
>> s1
set(['t', 'w', 'f'])
>> s2 = set(["python","fei"])
>> s2
set(['python', 'fei'])
>> s1.update(s2)
>> s1
set(['python', 'fei', 't', 'w', 'f'])
>> s2
set(['python', 'fei'])

pop

>> help(set.pop)
pop(...)
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.

例項10:

>> b_set = {"[1,2,3]","h","o","n","p","t","wtf","y"}
>> b_set.pop()
'[1,2,3]'
>> b_set.pop()
'wtf'
>> b_set.pop()
'h'
>> b_set
set(['o', 'n', 'p', 't', 'wtf', 'y'])
說明:從set中任意選一個刪除,並返回該值。
set是不能指定刪除某個元素的:
例項11:
>> b_set.pop("n")
Traceback (most recent call last):
File "", line 1, in
TypeError: pop() takes no arguments (1 given)

remove

>> help(set.remove)
remove(...)
Remove an element from a set; it must be a member.

If the element is not a member, raise a KeyError.

說明:set.remove(obj)中的obj必須是set中的元素,否則就報錯,實驗如下:
例項12:

>> b_set
set(['o', 'n', 'p', 't', 'wtf', 'y'])
>> b_set.remove("p")
>> b_set
set(['o', 'n', 't', 'wtf', 'y'])
>> b_set.remove("didi")
Traceback (most recent call last):
File "", line 1, in
KeyError: 'didi'
說明:明確告訴集合中沒有“wtf”。

discard(obj)

>> help(set.discard)
discard(...)
Remove an element from a set if it is a member.

If the element is not a member, do nothing.

說明:discard與remove類似,但是又有所不同,實驗如下:
例項13:

>> b_set
set(['o', 'n', 't', 'y'])
>> b_set.discard("n")
>> b_set
set(['o', 't', 'y'])
>> b_set.discard("wtf")
>> b_set
set(['o', 't', 'y'])
說明:discard(obj)中的obj,如果是集合中的元素,就刪除;如果不是,就什麼也不做,do nothing。
兩者做個對比:
例項14:
>> b_set
set(['o', 't', 'y'])
>> b_set.discard("w")
>> b_set
set(['o', 't', 'y'])
>> b_set.remove("w")
Traceback (most recent call last):
File "", line 1, in
KeyError: 'w'

clear

>> help(set.clear)
clear(...)
Remove all elements from this set.
例項15:
>> b_set
set(['o', 't', 'y'])
>> b_set.clear()
>> b_set
set([])
>> bool(b_set)  
False

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4422/viewspace-2803132/,如需轉載,請註明出處,否則將追究法律責任。

相關文章