Python 學習筆記(十)Python集合(二)

2022-07-19 22:36:22 字數 4622 閱讀 5372

集合常用的方法

add()       向集合中增加乙個元素,如果集合中已經有了這個元素,那個這個方法就會失效

1 >>>help(set.add)

2help on method_descriptor:34

add(...)

5add an element to a set. #向集合中新增乙個元素

67 this has no effect if the element is

already present. #集合中已經存在元素,則這個方式失效

89 >>> a =

10 >>>type(a)11'

set'>

12 >>> a.add("

weibo

") #向集合a中新增元素

15 >>>id(a) #集合a在記憶體中的位址

16 64656104l

17 >>> a.add("

ali"

) #向集合a中新增元素

])20 >>>id(a) #集合a中的記憶體位址沒有發生改變,是原地修改,是可變集合

21 64656104l

22 >>> a.add("

google

") #如果增加的元素在集合中存在,則不會做任何操作

23 >>> b ={} #建立乙個空的集合

24 >>> b.add("

python")

25traceback (most recent call last):

26 file "

", line 1, in

27 attributeerror: '

dict

' object has no attribute '

add' #報錯資訊為字典中沒有add函式

28 >>>type(b) #b是乙個字典29'

dict

'>

30 >>> b =set() #建立乙個空集合

31 >>>type(b)32'

set'>

33 >>> b.add("

python

") #向b中新增乙個元素

34 >>>b

35 set(['

python'])

36 >>> b.add([1,2,3]) #向b中新增乙個列表,報錯列表是不可hash的,是可改變的

37traceback (most recent call last):

38 file "

", line 1, in

39 typeerror: unhashable type: '

list

'40 >>> b.add((1,2,3)) #可以向集合中新增乙個元素

41 >>>b

42 set(['

python

', (1, 2, 3)])

43 >>>

update() 更新

1 >>>help(set.update)

2help on method_descriptor:34

update(...)

5 update a set with the union of itself and

others. #更新乙個集合,用這個集合本身和另外 引數裡面的內容轉換為集合

11 >>>a.update(b) #將集合b更新到a集合中

") #將乙個字串更新到集合a中

pop() 從集合中隨機刪除乙個元素,並且把這個元素作為返回值,pop函式沒有引數,不能指定元素

1 >>>help(set.pop)

2help on method_descriptor:34

pop(...)

5 remove and

return

an arbitrary set element. #從集合中移除乙個元素,並且把這個元素返回

6 raises keyerror if the set is

empty. #如果這個集合為空,那麼會報錯keyerror

78 >>>b

9 set(['

python

', (1, 2, 3)])

10 >>>b.pop()11'

python

'12 >>>b

13 set([(1, 2, 3)])

14 >>>

remove() 從集合中刪除指定的元素,刪除的元素必須是集合中的一員,如果不是,則會報錯keyerror

1 >>>help(set.remove)

2help on method_descriptor:34

remove(...)

5 remove an element from

a set; it must be a member. #從集合中刪除指定的元素,刪除的元素必須是集合中的一員

67 if the element is

not a member, raise

a keyerror. #如果不是集合中的元素,則會報錯keyerror

discard() 從集合中刪除指定的元素,刪除的元素必須是集合中的一員,如果不是,則不作任何操作

與remove()類似,區別就是remove() 刪除不是集合中的元素,則會報錯。而discard()刪除不是集合中的元素,則不會報錯。

示例:

1 >>>help(set.discard)

2help on method_descriptor:34

discard(...)

5 remove an element from a set if it is

a member.

67 if the element is

nota member, do nothing.

89 >>> a.discard("s"

)10 >>>

clear() 刪除集合中所有的元素

1 >>>help(set.clear)

2help on method_descriptor:34

clear(...)

5 remove all elements from

this set.

67 >>>a.clear()

8 >>>a #集合為乙個空集合

9set()

10 >>>

Python學習筆記(十)

mylab 專案實戰 1 在templates中乙個index.html我需要引入當前資料夾中的另乙個網頁,我直接在index的 中引入 html無效 最後,我在這個專案的主目錄下的urls中進行設定,可行 2 在呼叫網頁的時候,進行views設定,就已經把處理函式給選定了 直接在views,用re...

python學習筆記十

字典遍歷 集合函式 copy僅拷貝物件本身,而不對中的子物件進行拷貝,故對子物件進行修改也會隨著修改。dict1 dict2 dict1 dict3 dict1.copy dict1 user root dict1 num remove 1 print dict1 print dict2 print...

Python學習筆記(十) Python文件

以mark lutz著的 python學習手冊 為教程,每天花1個小時左右時間學習,爭取兩周完成。寫在前面的話 2013 7 20 20 00 學習筆記 1,python以 開始 注釋。python也支援可自動附加在物件上的文件,而且可以在執行時檢視。這類注釋是寫成字串,放在模組檔案 函式 類語句的...