在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ 數(shù)據(jù)結(jié)構(gòu)
文件和字符串
異常和異常類
Python面向?qū)ο蠛喗?/span>
面向?qū)ο蠼輳?/span>
對象序列化
數(shù)據(jù)結(jié)構(gòu)
開發(fā)環(huán)境設(shè)置
設(shè)計(jì)模式
類庫
構(gòu)建塊
繼承和多態(tài)
高級特性
Python面向?qū)ο蠼坛?/span>

數(shù)據(jù)結(jié)構(gòu)

從語法的角度來看,Python數(shù)據(jù)結(jié)構(gòu)非常直觀,它們提供了大量操作選擇。 您需要根據(jù)數(shù)據(jù)所涉及的內(nèi)容,是否需要修改,或者它是否為固定數(shù)據(jù)以及需要什么訪問類型(如開始/結(jié)束/隨機(jī)等)來選擇Python數(shù)據(jù)結(jié)構(gòu)。

列表(List)

列表(List)代表了Python中最通用的數(shù)據(jù)結(jié)構(gòu)類型。 列表是一個(gè)容器,它在方括號中包含逗號分隔的值(項(xiàng)目或元素)。 當(dāng)我們想要處理多個(gè)相關(guān)的值時(shí),列表很有用。 當(dāng)列表將數(shù)據(jù)保存在一起時(shí),我們可以一次對多個(gè)值執(zhí)行相同的方法和操作。 列表索引從零開始,與字符串不同,列表是可變的。

數(shù)據(jù)結(jié)構(gòu) - 列表

>>>
>>> # Any Empty List
>>> empty_list = []
>>>
>>> # A list of String
>>> str_list = ['Life', 'Is', 'Beautiful']
>>> # A list of Integers
>>> int_list = [1, 4, 5, 9, 18]
>>>
>>> #Mixed items list
>>> mixed_list = ['This', 9, 'is', 18, 45.9, 'a', 54, 'mixed', 99, 'list']
>>> # To print the list
>>>
>>> print(empty_list)
[]
>>> print(str_list)
['Life', 'Is', 'Beautiful']
>>> print(type(str_list))
<class 'list'>
>>> print(int_list)
[1, 4, 5, 9, 18]
>>> print(mixed_list)
['This', 9, 'is', 18, 45.9, 'a', 54, 'mixed', 99, 'list']

訪問Python列表中的項(xiàng)目
列表中的每個(gè)項(xiàng)目都分配一個(gè)數(shù)字 - 即該數(shù)字的索引或位置。索引總是從零開始,第二個(gè)索引是一個(gè),依此類推。 要訪問列表中的項(xiàng)目,我們可以在方括號內(nèi)使用這些索引編號。 例如,請注意以下代碼 -

>>> mixed_list = ['This', 9, 'is', 18, 45.9, 'a', 54, 'mixed', 99, 'list']
>>>
>>> # To access the First Item of the list
>>> mixed_list[0]
'This'
>>> # To access the 4th item
>>> mixed_list[3]
18
>>> # To access the last item of the list
>>> mixed_list[-1]
'list'

空的對象

空對象是最簡單和最基本的Python內(nèi)置類型。多次使用它們而沒有注意到并將它擴(kuò)展到我們創(chuàng)建的每個(gè)類。編寫一個(gè)空類的主要目的是暫時(shí)阻止某些東西,然后擴(kuò)展并向其添加行為。

將行為添加到類意味著用對象替換數(shù)據(jù)結(jié)構(gòu)并更改對其的所有引用。 因此,在創(chuàng)建任何內(nèi)容之前,檢查數(shù)據(jù)是否很重要,無論是偽裝的對象。 請注意以下代碼以獲得更好的理解:

>>> #Empty objects
>>>
>>> obj = object()
>>> obj.x = 9
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
obj.x = 9
AttributeError: 'object' object has no attribute 'x'

所以從上面可以看到,不可能在直接實(shí)例化的對象上設(shè)置任何屬性。當(dāng)Python允許對象具有任意屬性時(shí),它需要一定量的系統(tǒng)內(nèi)存來跟蹤每個(gè)對象具有的屬性,以存儲(chǔ)屬性名稱及其值。 即使沒有存儲(chǔ)屬性,也會(huì)為潛在的新屬性分配一定量的內(nèi)存。

因此Python默認(rèn)禁用對象和其他內(nèi)置插件的任意屬性。

>>> # Empty Objects
>>>
>>> class EmpObject:
    pass
>>> obj = EmpObject()
>>> obj.x = 'Hello, World!'
>>> obj.x
'Hello, World!'

因此,如果想將屬性組合在一起,可以將它們存儲(chǔ)在一個(gè)空對象中,如上面的代碼所示。 但是,并不建議這種方法。 請記住,只有在您要指定數(shù)據(jù)和行為時(shí)才應(yīng)使用類和對象。

元組

元組與元素類似,可以存儲(chǔ)元素。 但是,它們是不可變的,所以不能添加,刪除或替換對象。 元組提供的主要好處是因?yàn)樗牟蛔冃裕梢詫⑺鼈冇米髯值渲械逆I,或者在對象需要散列值的其他位置使用它們。

元組用于存儲(chǔ)數(shù)據(jù),而不是行為。 如果需要行為來操作元組,則需要將元組傳遞到執(zhí)行操作的函數(shù)(或另一個(gè)對象上的方法)。

由于元組可以作為字典鍵,所存儲(chǔ)的值彼此不同。 可以通過用逗號分隔值來創(chuàng)建一個(gè)元組。 元組包裹在圓括號中,但不是強(qiáng)制性的。 以下代碼顯示了兩個(gè)相同的分配。

>>> stock1 = 'MSFT', 95.00, 97.45, 92.45
>>> stock2 = ('MSFT', 95.00, 97.45, 92.45)
>>> type (stock1)
<class 'tuple'>
>>> type(stock2)
<class 'tuple'>
>>> stock1 == stock2
True
>>>

定義一個(gè)元組

元組與元素列表非常相似,只是整個(gè)元素集包含在圓括號中而不是方括號中。

就像當(dāng)對一個(gè)列表進(jìn)行切片時(shí)一樣,會(huì)得到一個(gè)新的列表,當(dāng)對一個(gè)元組進(jìn)行切片時(shí),會(huì)得到一個(gè)新的元組。

>>> tupl = ('Tuple','is', 'an','IMMUTABLE', 'list')
>>> tupl
('Tuple', 'is', 'an', 'IMMUTABLE', 'list')
>>> tupl[0]
'Tuple'
>>> tupl[-1]
'list'
>>> tupl[1:3]
('is', 'an')

Python元組方法
以下代碼顯示了Python元組的方法 -

>>> tupl
('Tuple', 'is', 'an', 'IMMUTABLE', 'list')
>>> tupl.append('new')
Traceback (most recent call last):
   File "<pyshell#148>", line 1, in <module>
      tupl.append('new')
AttributeError: 'tuple' object has no attribute 'append'
>>> tupl.remove('is')
Traceback (most recent call last):
   File "<pyshell#149>", line 1, in <module>
      tupl.remove('is')
AttributeError: 'tuple' object has no attribute 'remove'
>>> tupl.index('list')
4
>>> tupl.index('new')
Traceback (most recent call last):
   File "<pyshell#151>", line 1, in <module>
      tupl.index('new')
ValueError: tuple.index(x): x not in tuple
>>> "is" in tupl
True
>>> tupl.count('is')
1

從上面顯示的代碼中,我們可以知道元組是不可變的,因此 -

  • 不能將元素添加到元組中。
  • 不能附加或擴(kuò)展一個(gè)方法。
  • 不能從元組中移除元素。
  • 元組沒有刪除或彈出方法。
  • 計(jì)數(shù)和索引是元組中可用的方法。

字典

Dictionary是Python內(nèi)置的數(shù)據(jù)類型之一,它定義了鍵和值之間的一對一關(guān)系。

定義詞典
觀察以下代碼以了解有關(guān)定義字典的信息 -

>>> # empty dictionary
>>> my_dict = {}
>>>
>>> # dictionary with integer keys
>>> my_dict = { 1:'msft', 2: 'IT'}
>>>
>>> # dictionary with mixed keys
>>> my_dict = {'name': 'Aarav', 1: [ 2, 4, 10]}
>>>
>>> # using built-in function dict()
>>> my_dict = dict({1:'msft', 2:'IT'})
>>>
>>> # From sequence having each item as a pair
>>> my_dict = dict([(1,'msft'), (2,'IT')])
>>>
>>> # Accessing elements of a dictionary
>>> my_dict[1]
'msft'
>>> my_dict[2]
'IT'
>>> my_dict['IT']
Traceback (most recent call last):
   File "<pyshell#177>", line 1, in <module>
   my_dict['IT']
KeyError: 'IT'
>>>

從上面的代碼可以看到:

  • 首先創(chuàng)建一個(gè)包含兩個(gè)元素的字典并將其分配給變量my_dict。 每個(gè)元素都是一個(gè)鍵 - 值對,整個(gè)元素集都用大括號括起來。
  • 數(shù)字1是鍵,msft是它的值。 同樣,2是鍵,it是它的值。
  • 可以通過鍵獲取值,但反之亦然。 因此,當(dāng)我們嘗試my_dict ['it']時(shí),它會(huì)引發(fā)一個(gè)例外,因?yàn)?code>'it'是一個(gè)不存在的鍵。

修改字典

觀察以下代碼以了解有關(guān)修改字典的信息 -

>>> # Modifying a Dictionary
>>>
>>> my_dict
{1: 'msft', 2: 'IT'}
>>> my_dict[2] = 'Software'
>>> my_dict
{1: 'msft', 2: 'Software'}
>>>
>>> my_dict[3] = 'Microsoft Technologies'
>>> my_dict
{1: 'msft', 2: 'Software', 3: 'Microsoft Technologies'}

從上面的代碼可以觀察到 -

  • 字典中不能有重復(fù)的鍵。 更改現(xiàn)有鍵的值將刪除舊值。
  • 可以隨時(shí)添加新的鍵值對。
  • 字典在元素之間沒有順序的概念,它們是簡單的無序集合。

在字典中混合數(shù)據(jù)類型

觀察以下代碼以了解如何在字典中混合數(shù)據(jù)類型 -

>>> # Mixing Data Types in a Dictionary
>>>
>>> my_dict
{1: 'msft', 2: 'Software', 3: 'Microsoft Technologies'}
>>> my_dict[4] = 'Operating System'
>>> my_dict
{1: 'msft', 2: 'Software', 3: 'Microsoft Technologies', 4: 'Operating System'}
>>> my_dict['Bill Gates'] = 'Owner'
>>> my_dict
{1: 'msft', 2: 'Software', 3: 'Microsoft Technologies', 4: 'Operating System',
'Bill Gates': 'Owner'}

從上面的代碼可以觀察到 -

  • 字典值可以是任何數(shù)據(jù)類型,包括字符串,整數(shù),包括字典本身。
  • 與字典值不同,字典的鍵更受限制,但可以是任何類型的字符串,整數(shù)或任何其他類型。

從字典中刪除項(xiàng)目

觀察以下代碼以了解如果從字典中刪除項(xiàng)目 -

>>> # Deleting Items from a Dictionary
>>>
>>> my_dict
{1: 'msft', 2: 'Software', 3: 'Microsoft Technologies', 4: 'Operating System',
'Bill Gates': 'Owner'}
>>>
>>> del my_dict['Bill Gates']
>>> my_dict
{1: 'msft', 2: 'Software', 3: 'Microsoft Technologies', 4: 'Operating System'}
>>>
>>> my_dict.clear()
>>> my_dict
{}

從上面的代碼可以觀察到 -

  • del - 允許通過鍵從字典中刪除單個(gè)項(xiàng)目。
  • clear - 從字典中刪除所有項(xiàng)目。

集合

Set()是一個(gè)沒有重復(fù)元素的無序集合。 雖然單個(gè)項(xiàng)目是不可變的,但設(shè)置本身是可變的,也就是說可以添加或刪除組中的元素/項(xiàng)目??梢詧?zhí)行像聯(lián)合,交叉等數(shù)學(xué)運(yùn)算。

盡管集合通??梢允褂脴鋪韺?shí)現(xiàn),但可以使用哈希表來實(shí)現(xiàn)在Python中設(shè)置。 這允許它高度優(yōu)化的方法來檢查集合中是否包含特定的元素

創(chuàng)建一個(gè)集合
通過將所有項(xiàng)目(元素)放置在大括號{}內(nèi),用逗號或使用內(nèi)置函數(shù)set()分隔來創(chuàng)建一個(gè)集合。 遵守以下幾行代碼 -

>>> #set of integers
>>> my_set = {1,2,4,8}
>>> print(my_set)
{8, 1, 2, 4}
>>>
>>> #set of mixed datatypes
>>> my_set = {1.0, "Hello World!", (2, 4, 6)}
>>> print(my_set)
{1.0, (2, 4, 6), 'Hello World!'}
>>>

集合方法

觀察以下代碼以了解有關(guān)集合的方法 -

>>> >>> #METHODS FOR SETS
>>>
>>> #add(x) Method
>>> topics = {'Python', 'Java', 'C#'}
>>> topics.add('C++')
>>> topics
{'C#', 'C++', 'Java', 'Python'}
>>>
>>> #union(s) Method, returns a union of two set.
>>> topics
{'C#', 'C++', 'Java', 'Python'}
>>> team = {'Developer', 'Content Writer', 'Editor','Tester'}
>>> group = topics.union(team)
>>> group
{'Tester', 'C#', 'Python', 'Editor', 'Developer', 'C++', 'Java', 'Content
Writer'}
>>> # intersets(s) method, returns an intersection of two sets
>>> inters = topics.intersection(team)
>>> inters
set()
>>>
>>> # difference(s) Method, returns a set containing all the elements of
invoking set but not of the second set.
>>>
>>> safe = topics.difference(team)
>>> safe
{'Python', 'C++', 'Java', 'C#'}
>>>
>>> diff = topics.difference(group)
>>> diff
set()
>>> #clear() Method, Empties the whole set.
>>> group.clear()
>>> group
set()
>>>

集合的操作符
觀察以下代碼以了解有關(guān)運(yùn)算符的集合 -

>>> # PYTHON SET OPERATIONS
>>>
>>> #Creating two sets
>>> set1 = set()
>>> set2 = set()
>>>
>>> # Adding elements to set
>>> for i in range(1,5):
   set1.add(i)
>>> for j in range(4,9):
   set2.add(j)
>>> set1
{1, 2, 3, 4}
>>> set2
{4, 5, 6, 7, 8}
>>>
>>> #Union of set1 and set2
>>> set3 = set1 | set2 # same as set1.union(set2)
>>> print('Union of set1 & set2: set3 = ', set3)
Union of set1 & set2: set3 = {1, 2, 3, 4, 5, 6, 7, 8}
>>>
>>> #Intersection of set1 & set2
>>> set4 = set1 & set2 # same as set1.intersection(set2)
>>> print('Intersection of set1 and set2: set4 = ', set4)
Intersection of set1 and set2: set4 = {4}
>>>
>>> # Checking relation between set3 and set4
>>> if set3 > set4: # set3.issuperset(set4)
   print('Set3 is superset of set4')
elif set3 < set4: #set3.issubset(set4)
   print('Set3 is subset of set4')
else: #set3 == set4
   print('Set 3 is same as set4')
Set3 is superset of set4
>>>
>>> # Difference between set3 and set4
>>> set5 = set3 - set4
>>> print('Elements in set3 and not in set4: set5 = ', set5)
Elements in set3 and not in set4: set5 = {1, 2, 3, 5, 6, 7, 8}
>>>
>>> # Check if set4 and set5 are disjoint sets
>>> if set4.isdisjoint(set5):
   print('Set4 and set5 have nothing in common\n')
Set4 and set5 have nothing in common
>>> # Removing all the values of set5
>>> set5.clear()
>>> set5 set()