python數據結構

一:數據結構

  數據結構可以認爲他們是用來處理一些數據的或者說是存儲數據。

  對於數據結構的介紹會關係到類和對象的定義,此處對這兩個定義加以描述。

  何爲類:說道類首先我們能夠想到類型,在數據結構中類型有哪些常用的類型有int整型,float浮點型,等。在Python中類是有方法的,我們可以簡單理解爲對這一類可以執行哪些操作。

  何爲對象:對象就是實際定義的一個變量, i = 5 i的類型是int整型,對象就是i.


二:python數據結構的分類

  列表,元組,字典,序列。下面將分類介紹下各自的數據結構。


三:列表

  list是處理一組有序項目的數據結構,即你可以在一個列表中存儲一個 序列 的項目。假想你有一個購物列表,上面記載着你要買的東西,你就容易理解列表了。只不過在你的購物表上,可能每樣東西都獨自佔有一行,而在Python中,你在每個項目之間用逗號分割。

#!/usr/bin/python
# Filename: using_list.py
# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']
print 'I have', len(shoplist),'items to purchase.'
print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
    print item,
print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplist
print 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplist
print 'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist


變量shoplist是某人的購物列表。在shoplist中,我們只存儲購買的東西的名字字符串,但是記住,你可以在列表中添加 任何種類的對象 包括數甚至其他列表。


我們也使用了for..in循環在列表中各項目間遞歸。從現在開始,你一定已經意識到列表也是一個序列。


注意,我們在print語句的結尾使用了一個 逗號 來消除每個print語句自動打印的換行符。這樣做有點難看,不過確實簡單有效。


接下來,我們使用append方法在列表中添加了一個項目,就如前面已經討論過的一樣。然後我們通過打印列表的內容來檢驗這個項目是否確實被添加進列表了。打印列表只需簡單地把列表傳遞給print語句,我們可以得到一個整潔的輸出。


再接下來,我們使用列表的sort方法來對列表排序。需要理解的是,這個方法影響列表本身,而不是返回一個修改後的列表——這與字符串工作的方法不同。這就是我們所說的列表是 可變的 而字符串是 不可變的 。


最後,但我們完成了在市場購買一樣東西的時候,我們想要把它從列表中刪除。我們使用del語句來完成這個工作。這裏,我們指出我們想要刪除列表中的哪個項目,而del語句爲我們從列表中刪除它。我們指明我們想要刪除列表中的第一個元素,因此我們使用del shoplist[0](記住,Python從0開始計數)


輸出結果爲

$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']


四:元組

 元組和列表十分類似,只不過元組和字符串一樣是 不可變的 即你不能修改元組。元組通過圓括號中用逗號分割的項目定義。元組通常用在使語句或用戶定義的函數能夠安全地採用一組值的時候,即被使用的元組的值不會改變。

#!/usr/bin/python
# Filename: using_tuple.py
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]


len(zoo)是獲取元組的長度,說明元組是一個序列。


定義的new_zoo 是新創建一個元組,應爲元組是一個不可變得,不能再原處直接改變,需要另定義一個新的元組,新的元組內有疊加了一個zoon的元組。


獲取新的元組序列。


輸出新的元組序列


獲取新元組序列內的第3個序列,應爲是從0開始計數的。


獲取第三個元組內的第三個元素。


輸出結果

$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin


五:字典

  字典類似於有一個鍵值通過這個鍵值查找對應的信息。注意,鍵必須是唯一的

 注意,你只能使用不可變的對象(比如字符串)來作爲字典的鍵,但是你可以使用可變或不可變的對象作爲字典的值。基本說來就是,你應該只使用簡單的對象作爲鍵。鍵值對在字典中以這樣的方式標記:d = {key1 : value1, key2 : value2 }。注意它們的鍵/值對用冒號分割,而各個對用逗號分割,所有這些都包括在花括號中。

記住字典中的鍵/值對是沒有順序的。如果你想要一個特定的順序,那麼你應該在使用前自己對它們排序。字典是dict類的實例/對象。


#!/usr/bin/python
# Filename: using_dict.py
# 'ab' is short for 'a'ddress'b'ook
ab = {       'Swaroop'   : '[email protected]',
             'Larry'     : '[email protected]',
             'Matsumoto' : '[email protected]',
             'Spammer'   : '[email protected]'
     }
print "Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = '[email protected]'
# Deleting a key/value pair
del ab['Spammer']
print '\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
    print 'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')
    print "\nGuido's address is %s" % ab['Guido']


字典爲ab 輸出字典ab內鍵值是Swaroop


添加一個鍵值是 Guido:[email protected]


刪除一個鍵值是 Sapmmer


輸出字典ab的序列個數


把字典對應的改變成數組。


如果字典ab有Guido 那麼輸出該字典


輸出結果

$ python using_dict.py
Swaroop's address is [email protected]
There are 4 contacts in the address-book
Contact Swaroop at [email protected]
Contact Matsumoto at [email protected]
Contact Larry at [email protected]
Contact Guido at [email protected]
Guido's address is [email protected]

六:序列

列表、元組和字符串都是序列,但是序列是什麼,它們爲什麼如此特別呢?序列的兩個主要特點是索引操作符和切片操作符。索引操作符讓我們可以從序列中抓取一個特定項目。切片操作符讓我們能夠獲取序列的一個切片,即一部分序列。 字典不是序列。


#!/usr/bin/python
# Filename: seq.py
shoplist = ['apple', 'mango', 'carrot', 'banana']
# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]
# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]
# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]


序列切片從0 開始,-1代表着倒數 也是最後一個。

shoplist[1:3] 代表從1序列開始到3序列

shoplist[:] 代表所有序列

shoplist[:-1] 代表除了最後一個所有的序列。


六:參考


當你創建一個對象並給它賦一個變量的時候,這個變量僅僅 參考 那個對象,而不是表示這個對象本身!也就是說,變量名指向你計算機中存儲那個對象的內存。這被稱作名稱到對象的綁定


wKioL1Tu_JLDdFEqAABORQIZLi4743.jpg

變量一等於變量二。其實就是改下變量二的指針指向變量一的對象,如果變量一發生改變那麼變量二也會跟着改變。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章