python數據結構

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

>>> shoplist=["apple","mango","banner"]
>>> print ('I have',len(shoplist),'items to puchase')
I have 3 items to puchase
>>> print ('These items are:')
These items are:
>>> for item in shoplist:
...     
...     print (item),
... 
apple
(None,)
mango
(None,)
banner
(None,)
>>> print ('\n also have to buy rice')

 also have to buy rice
>>> shoplist.append('rice')
>>> print ('My shopping list is now',shoplist)
My shopping list is now ['apple', 'mango', 'banner', 'rice']
>>> print ('I will sort my list now')
I will sort my list now
>>> shoplist.sort()
>>> print ("SORTED SHHOPPING LIST IS:",shoplist)
SORTED SHHOPPING LIST IS: ['apple', 'banner', 'mango', 'rice']
>>> print ('The first item i will buy is',shoplist[0])
The first item i will buy is apple
>>> olditem=shoplist[0]
>>> del shoplist[0]
>>> print ("I bought the:"olditem)
  File "<stdin>", line 1
    print ("I bought the:"olditem)
                                ^
SyntaxError: invalid syntax
>>> print ("I bought the:",olditem)
I bought the: apple
>>> ptint ("Myshopping list is now",shoplist)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ptint' is not defined
>>> print ("Myshopping list is now",shoplist) 
Myshopping list is now ['banner', 'mango', 'rice']
>>> 

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

>>> zoo=('woolf','elephant','penguin')
>>> print ('number of animals in the zoo is',len(zoo))
number of animals in the zoo is 3
>>> new zoo=("monkey","dolphin",zoo)
  File "<stdin>", line 1
    new zoo=("monkey","dolphin",zoo)
          ^
SyntaxError: invalid syntax
>>> new_zoo=("monkey","dolphin",zoo)
>>> print ("Numner of animals in new zoo are",new_zoo)
Numner of animals in new zoo are ('monkey', 'dolphin', ('woolf', 'elephant', 'penguin'))
>>> print ("All animals in new zoo are",len(new_zoo))
All animals in new zoo are 3
>>> print ("Animals brought form old zoo are",new_zoo[2])
Animals brought form old zoo are ('woolf', 'elephant', 'penguin')
>>> print ("last animal brought from old zoo is",new_zoo[2][2])
last animal brought from old zoo is penguin
>>> 

字典
字典類似於你通過聯繫人名字查找地址和聯繫人詳細情況的地址簿,即,我們把鍵(名字)和
值(詳細情況)聯繫在一起。注意,鍵必須是唯一的,就像如果有兩個人恰巧同名的話,你無
法找到正確的信息。注意,你只能使用不可變的對象(比如字符串)來作爲字典的鍵,但是你可以把不可變或可變的對象作爲字典的值。基本說來就是,你應該只使用簡單的對象作爲鍵。鍵值對在字典中以這樣的方式標記:d = {key1 : value1, key2 : value2 }。注意它們的鍵/值對用冒 號分割,而各個對用逗號分割,所有這些都包括在花括號中。
記住字典中的鍵/值對是沒有順序的。如果你想要一個特定的順序,那麼你應該在使用前自己 對它們排序。字典是dict類的實例/對象

>>> ab={'love':'[email protected]','li':'[email protected],'rui':[email protected],'rui1':[email protected] 

>>> 
>>> 
>>> ab={'love':'[email protected]','rui':'[email protected]','peng':'[email protected]'}
>>> print ("love mean is %s"%ab['love'])                             
love mean is [email protected]
>>> ab['li']='li#[email protected]'
>>> del ab['peng']
>>> print ("\nThere are %d loves in the sentence\n"%len(ab))

There are 3 loves in the sentence

>>> for name,address in ab.items():
...     
...          
...     print("Contact %s at %s"%(name,address))
... 
Contact love at [email protected]
Contact rui at [email protected]
Contact li at li#[email protected]
>>> if "li" in ab:
...     
...     print ("\n li address is %s"%ab[li])
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'li' is not defined
>>> if 'li' in ab:                          
...     
...     print('address is %s'%ab['li'])
... 
address is li#[email protected]
>>> 

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

shoplist=['apple','banner','mango']
>>> print ('item 0 is',shoplist[0])
item 0 is apple
>>> print ('item 1 to 3 is',shoplist[1:3])
item 1 to 3 is ['banner', 'mango']
>>> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章