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']
>>> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章