Python語言組合數(shù)據(jù)類型ppt課件_第1頁
Python語言組合數(shù)據(jù)類型ppt課件_第2頁
Python語言組合數(shù)據(jù)類型ppt課件_第3頁
Python語言組合數(shù)據(jù)類型ppt課件_第4頁
Python語言組合數(shù)據(jù)類型ppt課件_第5頁
已閱讀5頁,還剩53頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、 第四章 組合數(shù)據(jù)類型4.1 列表4.2 元組4.3 字典4.4 集合4.5 實驗4.6 小結(jié)習題4.1 列表列表(Lists)屬于Python中的序列類型,它是任意對象的有序集合,通過“位置”或者“索引”訪問其中的元素,它具有可變對象、可變長度、異構(gòu)和任意嵌套的特點。第四章 組合數(shù)據(jù)類型4.1.1 創(chuàng)建列表列表里第一個元素的“位置”或者“索引”是從“0”開始,第二個元素的則是“1”,以此類推。在創(chuàng)建列表時,列表元素放置在方括號 中,以逗號來分隔各元素,格式如下:listname = 元素1, 元素2, 元素3, , 元素n舉例如下:sample_list1 = 0, 1, 2, 3, 4sa

2、mple_list2 = P, y, t, h, o, nsample_list3 = Python, sample, list, for, your, reference4.1.1 創(chuàng)建列表第四章 組合數(shù)據(jù)類型4.1 列表代碼運行如下: sample_list1 = 0, 1, 2, 3, 4 #列表sample_list1 sample_list2 = P, y, t, h, o, n #列表sample_list2 sample_list3 = Python, sample, list, for, your, reference #列表sample_list3 print (sample

3、_list1) #打印輸出列表0, 1, 2, 3, 4 #輸出結(jié)果 print (sample_list2) #打印輸出列表p, y, t, h, o, n #輸出結(jié)果 print (sample_list3) #打印輸出列表Python, sample, list, for, your, reference #輸出結(jié)果4.1.1 創(chuàng)建列表第四章 組合數(shù)據(jù)類型4.1 列表列表中允許有不同數(shù)據(jù)類型的元素,例如:sample_list4 = 0, y, 2, h, 4, n, Python但通常建議列表中元素最好使用相同的數(shù)據(jù)類型。列表可以嵌套使用,例如:sample_list5 = sampl

4、e_list1, sample_list2, sample_list3運行結(jié)果如下: sample_list1 = 0, 1, 2, 3, 4 sample_list2 = P, y, t, h, o, n sample_list3 = Python, sample, list, for, your, reference sample_list5 = sample_list1, sample_list2, sample_list3 #創(chuàng)建一個嵌套列表 print (sample_list5)0, 1, 2, 3, 4, P, y, t, h, o, n, Python, sample, lis

5、t, for, your, reference4.1.1 創(chuàng)建列表第四章 組合數(shù)據(jù)類型4.1 列表通過使用“位置”或者“索引”來訪問列表中的值,將放在方括號中。特別注意,“位置”或者“索引”是從0開始,例如引用上一節(jié)列表示例 sample_list1中的第1個,可以寫成:sample_list10;引用第3個值,可以寫成:sample_list12。代碼示例為: sample_list1 = 0, 1, 2, 3, 4 print (sample_list10: , sample_list10) #輸出索引為0的元素sample_list10: 0 print (sample_list12:

6、, sample_list12) #輸出索引為2的元素sample_list12: 24.1.2 使用列表 第四章 組合數(shù)據(jù)類型4.1 列表可以在方括號中使用“負整數(shù)”,如:sample_list1-2,意為從列表的右側(cè)開始倒數(shù)2個的元素,即索引倒數(shù)第2的元素。 sample_list1 = 0, 1, 2, 3, 4 print (sample_list1-2: , sample_list1-2)#輸出索引倒數(shù)第2的元素sample_list1-2: 34.1.2 使用列表 第四章 組合數(shù)據(jù)類型4.1 列表以在方括號中用冒號分開的兩個整數(shù)來截取列表中的元素,例如sample_list22:4

7、,可取得列表sample_list2中的第3個和第4個元素,不包含第5個元素。 sample_list2 = p, y, t, h, o, n print (sample_list22:4:, sample_list22:4)sample_list22:4: t, h該類操作被稱為“切片”操作(slice)。4.1.2 使用列表 第四章 組合數(shù)據(jù)類型4.1 列表對列表的元素進行修改時,可以使用賦值語句: sample_list3 = python, sample, list, for, your, reference sample_list34 = my print (sample_list3

8、4:, sample_list34)sample_list34: my print (sample_list3:, sample_list3)sample_list3: python, sample, list, for, my, reference4.1.2 使用列表 第四章 組合數(shù)據(jù)類型4.1 列表刪除列表的元素,可以使用 del 語句,格式為:del listname索引該索引的元素被刪除后,后面的元素將會自動移動并填補該位置。4.1.3 刪除列表元素在不知道或不關(guān)心元素的索引時,可以使用列表內(nèi)置方法remove()來刪除指定的值,例如:listname.remove(值)清空列表,可以

9、采用重新創(chuàng)建一個與原列表名相同的空列表的方法,例如:listname = 刪除整個列表,也可以使用del語句,格式為:del listname第四章 組合數(shù)據(jù)類型4.1 列表代碼示例如下: sample_list4 = 0, y, 2, h, 4, n, Python del sample_list45 #刪除列表中索引為5的元素 print (after deletion, sample_list4: , sample_list4) after deletion, sample_list4: 0, y, 2, h, 4, Python sample_list4.remove(Python)

10、#刪除列表中值為Python的元素 print (after removing, sample_list4: , sample_list4)after removing, sample_list4: 0, y, 2, h, 4 sample_list4 = #重新創(chuàng)建列表并置為空 print (sample_list4) #輸出該列表 del sample_list4 #刪除整個列表 print (sample_list4) #打印輸出整個列表Traceback (most recent call last): File , line 1, in print (sample_list4)Nam

11、eError: name sample_list4 is not defined #系統(tǒng)報告該列表未定義4.1.3 刪除列表元素第四章 組合數(shù)據(jù)類型4.1 列表代碼示例如下: sample_list1 = 0, 1, 2, 3, 4 len(sample_list1) #列表的元素數(shù)量5 max(sample_list1) #列表中元素的最大值4 min(sample_list1) #列表中元素的最小值04.1.4 列表的內(nèi)置函數(shù)與其他方法注意:Python 3 中已經(jīng)沒有了Python 2中用于列表比較的cmp函數(shù)。第四章 組合數(shù)據(jù)類型4.1 列表4.1.4 列表的內(nèi)置函數(shù)與其他方法方法說明

12、listname.append(元素)添加新的元素在列表末尾listname.count(元素)統(tǒng)計該元素在列表中出現(xiàn)的次數(shù)listname.extend(序列)追加另一個序列類型中的多個值,到該列表末尾(用新列表擴展原來的列表)listname.index(元素)從列表中找出某個值第一個匹配元素的索引位置listname.insert(位置, 元素)將元素插入列表listname.pop(index=-1)移除列表中的一個元素(“-1”表示從右側(cè)數(shù)第一個元素,也就是最后一個索引的元素),并且返回該元素的值listname.remove(元素)移除列表中的第一個匹配某個值的元素listname

13、.reverse()將列表中元素反向l i s t n a m e . s o r t ( c m p = N o n e , key=None, reverse=False)對列表進行排序listname.clear()清空列表listname.copy()復制列表第四章 組合數(shù)據(jù)類型4.1 列表 第四章 組合數(shù)據(jù)類型4.1 列表4.2 元組4.3 字典4.4 集合4.5 實驗4.6 小結(jié)習題4.2 元組元組(Tuples)與列表一樣,屬于Python中的序列類型,它是任意對象的有序集合,通過“位置”或者“索引”訪問其中的元素,它具有可變長度、異構(gòu)和任意嵌套的特點,與列表不同的是:元組中的元

14、素是不可修改的。第四章 組合數(shù)據(jù)類型4.2.1 創(chuàng)建元組元組的創(chuàng)建很簡單,把元素放入小括號,并在每兩個元素中間使用逗號隔開即可,格式為:tuplename = (元素1, 元素2, 元素3, , 元素n)舉例如下:sample_tuple1 = (1, 2, 3, 4, 5, 6)sample_tuple2 = p, y, t, h, o, nsample_tuple3 = (python, sample, tuple, for, your, reference)sample_tuple4 = (python, sample, tuple, 1989, 1991, 2018)4.2.1 創(chuàng)建元

15、組第四章 組合數(shù)據(jù)類型4.2 元組元組也可以為空:sample_tuple5 = ()需要注意的是,為避免歧義,當元組中只有一個元素時,必須在該元素后加上逗號,否則括號會被當作運算符,例如:sample_tuple6 = (123,)4.2.1 創(chuàng)建元組元組中的元素可以是各種可迭代的數(shù)據(jù)類型。第四章 組合數(shù)據(jù)類型4.2 元組元組也可以嵌套使用,例如: sample_tuple1 = (1, 2, 3, 4, 5, 6) sample_tuple2 = P, y, t, h, o, n sample_tuple7 = (sample_tuple1, sample_tuple2) print (s

16、ample_tuple7)(1, 2, 3, 4, 5, 6), (P, y, t, h, o, n)4.2.1 創(chuàng)建元組第四章 組合數(shù)據(jù)類型4.2 元組與列表相同,我們可以通過使用“位置”或者“索引”來訪問元組中的值,“位置”或者“索引”也是是從0開始,例如:sample_tuple1 = (1, 2, 3, 4, 5, 6)sample_tuple11表示元組tuple1中的第2個元素:2。sample_tuple13:5 表示元組sample_tuple1中的第4個和第5個元素,不包含第6個元素:4,5。sample_tuple1-2 表示元組sample_tuple1中從右側(cè)向左數(shù)的第

17、2個元素:5。4.2.2 使用元組第四章 組合數(shù)據(jù)類型4.2 元組代碼示例為: sample_tuple1 = (1, 2, 3, 4, 5, 6) print (sample_tuple11) #截取第2個元素2 print (sample_tuple13:5) #第4個和第5個元素,不包含第6個元素(4, 5) print (sample_tuple1-2) #從右側(cè)向左數(shù)的第2個元素54.2.2 使用元組第四章 組合數(shù)據(jù)類型4.2 元組元組也支持“切片”操作,例如sample_tuple2 = P, y, t, h, o, nsample_tuple2: 表示取元組sample_tupl

18、e2的所有元素;sample_tuple23: 表示取元組sample_tuple2的索引為3的元素之后的所有元素;sample_tuple20:4:2 表示元組sample_tuple2的索引為0到4的元素,每隔一個元素取一個。4.2.2 使用元組第四章 組合數(shù)據(jù)類型4.2 元組代碼示例為: sample_tuple2 = P, y, t, h, o, n print (sample_tuple2:) #取元組sample_tuple2的所有元素(P, y, t, h, o, n) print (sample_tuple23:) #取元組的第4個元素之后的所有元素(h, o, n) prin

19、t (sample_tuple20:4:2) #元組sample_tuple2的第1個到第5元素,每隔一個元素取一個(P, t)4.2.2 使用元組第四章 組合數(shù)據(jù)類型4.2 元組由于元組中的元素是不可變的,也就是不允許被刪除的,但可以使用del 語句刪除整個元組:del tuple代碼示例如下: sample_tuple3 = (Python, sample, tuple, for, your, reference) print (sample_tuple3) #輸出刪除前的元組sample_tuple3(Python, sample, tuple, for, your, reference

20、) del sample_tuple3 #刪除元組sample_tuple3 print (sample_tuple3) #輸出刪除后的元組sample_tuple3Traceback (most recent call last): File , line 1, in print (sample_tuple3)NameError: name sample_tuple3 is not defined #系統(tǒng)正常報告sample_tuple3沒有定義4.2.3 刪除元組第四章 組合數(shù)據(jù)類型4.2 元組4.2.4 元組的內(nèi)置函數(shù)函數(shù)說明len(tuplename)返回元組的元素數(shù)量max(tupl

21、ename)返回元組中元素的最大值min(tuplename)返回元組中元素的最小值tuple(listname)將列表轉(zhuǎn)換為元組第四章 組合數(shù)據(jù)類型4.2 元組代碼示例如下: sample_tuple1 = (1, 2, 3, 4, 5, 6) #創(chuàng)建元組tuple1 print (len(sample_tuple1) #輸出元組長度6 print (max(sample_tuple1) #輸出元組最大值6 print (min(sample_tuple1) #輸出元組最小值1 a = 1,2,3 #創(chuàng)建列表a print (a) #輸出列表a1, 2, 3 print (tuple(a)

22、#轉(zhuǎn)換列表a為元組后輸出(1, 2, 3)4.2.4 元組的內(nèi)置函數(shù)第四章 組合數(shù)據(jù)類型4.2 元組 第四章 組合數(shù)據(jù)類型4.1 列表4.2 元組4.3 字典4.4 集合4.5 實驗4.6 小結(jié)習題4.3 字典字典(Dictionaries),屬于映射類型,它是通過鍵實現(xiàn)元素存取,具有無序、可變長度、異構(gòu)、嵌套和可變類型容器等特點。第四章 組合數(shù)據(jù)類型4.3.1 創(chuàng)建字典字典中的鍵和值有單引號,他們成對出現(xiàn),中間用冒號分割,每對直接用逗號分割,并放置在花括號中,格式如下:dictname = 鍵1: 值1, 鍵2: 值2, 鍵3: 值3, , 鍵n: 值n在同一個字典中,鍵應該是唯一的,但值則

23、無此限制。舉例如下:sample_dict1 = Hello: World, Capital: BJ, City: CQsample_dict2 = 12: 34, 34: 56, 56: 78sample_dict3 = Hello: World, 34: 56, City: CQ4.3.1 創(chuàng)建字典第四章 組合數(shù)據(jù)類型4.3 字典創(chuàng)建字典時,同一個鍵被兩次賦值,那么第一個值無效,第二個值被認為是該鍵的值。sample_dict4 = Model: PC, Brand: Lenovo, Brand: Thinkpad這里的鍵Brand生效的值是Thinkpad。4.3.1 創(chuàng)建字典第四章 組

24、合數(shù)據(jù)類型4.3 字典創(chuàng)建字典時,同一個鍵被兩次賦值,那么第一個值無效,第二個值被認為是該鍵的值。sample_dict4 = Model: PC, Brand: Lenovo, Brand: Thinkpad這里的鍵Brand生效的值是Thinkpad。4.3.1 創(chuàng)建字典第四章 組合數(shù)據(jù)類型4.3 字典字典也支持嵌套,格式如下:dictname = 鍵1: 鍵11: 值11, 鍵12: 值12 , 鍵2: 鍵21: 值21, 鍵2: 值22, , 鍵n: 鍵n1: 值n1, 鍵n2: 值n2例如:sample_dict5 = office: room1:Finance , room2:lo

25、gistics, lab:lab1:Physics, lab2:Chemistry4.3.1 創(chuàng)建字典第四章 組合數(shù)據(jù)類型4.3 字典使用字典中的值時,只需要把對應的鍵放入方括號,格式為:dictname鍵舉例如下: sample_dict1 = Hello: World, Capital: BJ, City: CQ print (sample_dict1Hello: , sample_dict1Hello) sample_dict1Hello: World #輸出鍵為Hello的值 sample_dict2 = 12: 34, 34: 56, 56: 78 print (sample_dic

26、t212: , sample_dict212) sample_dict212: 34 #輸出鍵為12的值4.3.2 使用字典第四章 組合數(shù)據(jù)類型4.3 字典使用包含嵌套的字典,例如: sample_dict5 = office: room1:Finance, room2:logistics, lab:lab1: Physics, lab2:Chemistry print (sample_dict5office: , sample_dict5office) sample_dict5office: room1: Finance, room2: logistics #輸出鍵為office 的值4.3

27、.2 使用字典第四章 組合數(shù)據(jù)類型4.3 字典可以對字典中的已有的值進行修改,例如: sample_dict1 = Hello: World, Capital: BJ, City: CQ print (sample_dict1City) #輸出鍵為City的值CQ sample_dict1City = NJ #把鍵為City的值修改為NJ print (sample_dict1City) #輸出鍵為City的值NJ print (sample_dict1)Hello: World, Capital: BJ, City: NJ #輸出修改后的字典4.3.2 使用字典第四章 組合數(shù)據(jù)類型4.3 字

28、典可以向字典末尾追加新的鍵值,例如: sample_dict1 = Hello: World, Capital: BJ, City: CQ sample_dict1viewspot= HongYaDong #把新的鍵和值添加到字典 print (sample_dict1) #輸出修改后的字典Hello: World, Capital: BJ, City: CQ, viewspot: HongYaDong4.3.2 使用字典第四章 組合數(shù)據(jù)類型4.3 字典可以使用del語句刪除字典中的鍵和對應的值,格式為:del dictname鍵使用del語句刪除字典,格式為:del dictname舉例如下

29、: sample_dict1 = Hello: World, Capital: BJ, City: CQ del sample_dict1City #刪除字典中的鍵City和對應的值 print (sample_dict1) #打印結(jié)果Hello: World, Capital: BJ del sample_dict1 #刪除該字典 print (sample_dict1) #打印該字典Traceback (most recent call last): #系統(tǒng)正常報錯,該字典未定義 File , line 1, in print (sample_dict1)NameError: name s

30、ample_dict1 is not defined4.3.3 刪除元素和字典第四章 組合數(shù)據(jù)類型4.3 字典舉例如下: sample_dict1 = Hello: World, Capital: BJ, City: CQ len(sample_dict1) #計算該字典中鍵的總數(shù)3 str(sample_dict1) #輸出字典Hello: World, Capital: BJ, City: CQ type(sample_dict1) #返回數(shù)據(jù)類型4.3.4 字典的內(nèi)置函數(shù)和方法函數(shù)說明len(distname)計算鍵的總數(shù)str(distname)輸出字典type(distname)返回

31、字典類型第四章 組合數(shù)據(jù)類型4.3 字典4.3.4 字典的內(nèi)置函數(shù)和方法方法說明dictname.clear()刪除字典所有元素,清空字典dictname.copy()以字典類型返回某個字典的淺復制dictname.fromkeys(seq, value)創(chuàng)建一個新字典,以序列中的元素做字典的鍵,值為字典所有鍵對應的初始值d i c t n a m e . g e t ( v a l u e , default=None)返回指定鍵的值,如果值不在字典中返回default值key in dictname如果鍵在字典dict里返回true,否則返回falsedictname.items()以列表

32、返回可遍歷的(鍵, 值) 元組數(shù)組dictname.keys()將一個字典所有的鍵生成列表并返回dictname.setdefault(value, default=None)和dictname.get()類似, 不同點是,如果鍵不存在于字典中,將會添加鍵并將值設為default對應的值dictname.update(dictname2)把字典dictname2的鍵/值對更新到dictname里dictname.values()以列表返回字典中的所有值dictname.pop(key,default)彈出字典給定鍵所對應的值,返回值為被刪除的值。鍵值必須給出。 否則,返回default值。di

33、ctname.popitem()彈出字典中的一對鍵和值(一般刪除末尾對),并刪除第四章 組合數(shù)據(jù)類型4.3 字典 第四章 組合數(shù)據(jù)類型4.1 列表4.2 元組4.3 字典4.4 集合4.5 實驗4.6 小結(jié)習題4.4 集合集合(set),是一種集合類型,可以理解為就是數(shù)學課里學習的集合。它是一個可以表示任意元素的集合,它的索引可以通過另一個任意鍵值的集合進行,它可以無序排列、哈希。集合分為兩類:可變集合(set),不可變集合(frozenset)??勺兗?,在被創(chuàng)建后,可以通過很多種方法被改變,例如add(),update()等。不可變集合,由于其不可變特性,它是可哈希的(hashable,意

34、為一個對象在其生命周期中,其哈希值不會改變,并可以和其他對象做比較),也可以作為一個元素被其他集合使用,或者作為字典的鍵。第四章 組合數(shù)據(jù)類型4.4.1 創(chuàng)建集合使用大括號 或者set()創(chuàng)建非空集合,格式為:sample_set = 值1, 值2, 值3, , 值n或sample_set = set(值1, 值2, 值3, , 值n)創(chuàng)建一個不可變集合,格式為:sample_set = frozenset(值1, 值2, 值3, , 值n)舉例如下:sample_set1 = 1, 2, 3, 4, 5sample_set2 = a, b, c, d, esample_set3 = Beij

35、ing, Tianjin, Shanghai, Nanjing, Chongqingsample_set4 = set(11, 22, 33, 44, 55)sample_set5 = frozenset(CHS, ENG, , , ,) #創(chuàng)建不可變集合但創(chuàng)建空集合時必須使用set(),格式:emptyset = set()4.4.1 創(chuàng)建集合第四章 組合數(shù)據(jù)類型4.4 集合集合的一個顯著的特點就是可以去掉重復的元素,例如: sample_set6 = 1, 2, 3, 4, 5, 1, 2, 3, 4, print (sample_set6) #輸出去掉重復的元素的集合1, 2, 3, 4

36、, 54.4.2 使用集合可以使用len()函數(shù)來獲得集合中元素的數(shù)量,例如: sample_set6 = 1, 2, 3, 4, 5, 1, 2, 3, 4, len(sample_set6) #輸出集合的元素數(shù)量5注意:這里集合的元素數(shù)量,是去掉重復元素之后的數(shù)量。第四章 組合數(shù)據(jù)類型4.4 集合集合是無序的,因此沒有“索引”或者“鍵”來指定調(diào)用某個元素,但可以使用for循環(huán)輸出集合的元素,例如: sample_set6 = 1, 2, 3, 4, 5, 1, 2, 3, 4, for x in sample_set6:print (x)123454.4.2 使用集合注意:這里輸出的集合的

37、元素,也是去掉重復元素之后的。第四章 組合數(shù)據(jù)類型4.4 集合向集合中添加一個元素,可以使用add()方法,即把需要添加的內(nèi)容作為一個元素(整體),加入到集合中,格式為:setname.add(元素)向集合中添加多個元素,可以使用update()方法,將另一個類型中的元素拆分后,添加到原集合中,格式為:setname.update(others)4.4.2 使用集合兩種增加集合元素的方法,對可變集合有效,例如: sample_set1 = 1, 2, 3, 4, 5 sample_set1.add(6) #使用add方法添加元素到集合 print (after being added, the

38、 set is: , sample_set1)after being added, the set is: 1, 2, 3, 4, 5, 6 sample_set1.update(python) #使用update方法添加另一個集合 print (after being updated, the set is:, sample_set1)after being updated, the set is: y, 1, 2, 3, 4, 5, 6, p, t, n, o, h, 第四章 組合數(shù)據(jù)類型4.4 集合集合可以被用來做成員測試,使用in或not in檢查某個元素是否屬于某個集合,例如: sa

39、mple_set1 = 1, 2, 3, 4, 5 sample_set2 = a, b, c, d, e 3 in sample_set1 #判斷3是否在集合中,是則返回TrueTrue c not in sample_set2 #判斷“c沒有在集合中” False #如果c在該集合中,返回False,#否則返回True4.4.2 使用集合第四章 組合數(shù)據(jù)類型4.4 集合集合之間可以做集合運算,求差集(difference)、并集(union)、交集(intersection)、對稱差集(symmetric difference) sample_set7 = C, D, E, F, G sa

40、mple_set8 = E, F, G, A, B sample_set7 - sample_set8 #差集D, C sample_set7 | sample_set8 #并集A, G, B, F, E, D, C sample_set7 & sample_set8 #交集E, G, F sample_set7 sample_set8 #對稱差集A, B, D, C4.4.2 使用集合第四章 組合數(shù)據(jù)類型4.4 集合可以使用remove()方法刪除集合中的元素,格式為:setname.remove(元素)可使用del方法刪除集合,格式為:del setname舉例如下: sample

41、_set1 = 1, 2, 3, 4, 5 sample_set1.remove(1) #使用remove方法刪除元素 print (sample_set1)2, 3, 4, 5 sample_set1.clear() print (sample_set1) #清空集合中的元素set() #返回結(jié)果為空集合 del sample_set1 #刪除集合 print (sample_set1)Traceback (most recent call last): #系統(tǒng)報告,該集合未定義 File , line 1, in print (sample_set1)NameError: name sam

42、ple_set1 is not defined4.4.3 刪除元素和集合第四章 組合數(shù)據(jù)類型4.4 集合4.4.4 集合的方法方法說明len(ss)返回集合的元素個數(shù)x in ss測試x是否是集合ss中的元素,返回True或Falsex not in ss如果x不在集合ss中,返回True,否則返回Falsess.isdisjoint(otherset)當集合ss與另一集合otherset不相交時,返回True,否則返回Falsess.issubset(otherset) 或 ss = otherset如果集合ss是另一集合otherset的子集,返回True,否則返回Falsess = ot

43、herset如果集合ss是另一集合otherset的父集,返回True,否則返回Falsess otherset如果集合ss是另一集合otherset的父集,且otherset是ss的子集,則返回True,否則返回Falsess.union(*othersets) 或ss | otherset1 | otherset2 返回ss和othersets的并集,包含有set和othersets的所有元素ersection(*othersets)或 ss & otherset1 & otherset2 返回ss和othersets的交集,包含在ss并且也在othersets

44、中的元素ss.difference(*othersets) 或 ss - otherset1 - otherset2 返回ss與othersets的差集,只包含在ss中但不在othersets中的元素ss.symmetric_difference(otherset) 或 set otherset返回ss與otherset的對稱差集,只包含在ss中但不在othersets中,和不在ss中但在othersets中的元素ss.copy()返回集合ss的淺拷貝第四章 組合數(shù)據(jù)類型4.4 集合4.4.4 集合的方法第四章 組合數(shù)據(jù)類型方法說明ss.update(*othersets) 或 ss |= otherset1 | otherset2 將另外的一個集合或多個集合元素,添加到集合ss中ersection_update(*othersets) 或 set &= otherset1 & otherset2 在ss中保留它與其他集合的交集ss.difference_update(*othersets) 或 ss -=

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論