python入门(七)
python數(shù)據(jù)結(jié)構(gòu)
一、數(shù)據(jù)結(jié)構(gòu)概述
數(shù)據(jù)組織在一起的結(jié)構(gòu)叫做數(shù)據(jù)結(jié)構(gòu)。
python中的數(shù)據(jù)結(jié)構(gòu):列表,元組,字典,隊列,棧,樹等等。
python內(nèi)置數(shù)據(jù)結(jié)構(gòu):列表、元組等。
python擴展數(shù)據(jù)結(jié)構(gòu):棧,隊列等。
數(shù)據(jù)結(jié)構(gòu)式靜態(tài)的,算法是動態(tài)的。
數(shù)據(jù)結(jié)構(gòu)示例:
1 #coding=utf-8 2 ''' 3 Created on 2016��4��20�� 4 5 @author: Administrator 6 ''' 7 #python內(nèi)置數(shù)據(jù)結(jié)構(gòu),元組、列表、字典 8 #將三個物品"apple"、"orange"、"pear"組織起來 9 #存儲方式一,可以取出來(列表) 10 a=["apple","orange","pear"] 11 print a 12 #存儲方式二,不可以取出來,不可以改變(元組) 13 b=("apple","orange","pear") 14 print b 15 #存儲方式三,不僅按順序存儲,存儲空間還有名字 16 c={"sam":"apple","jac":"orage","mating":"pear"} 17 print c?
?
二、Python 常見數(shù)據(jù)結(jié)構(gòu)-棧
棧是一端開口,一端開放的容器。
代碼示例:
1 #coding=utf-8 2 #棧 3 class Stack: 4 #初始化棧 5 def __init__(self,size):#棧的主體,與棧的容量 6 self.stack=[]#聲明棧的屬性,此時棧的屬性為列表 7 self.size=size#傳遞棧的容量 8 self.top=-1#初始化棧頂?shù)奈恢?#xff0c;有數(shù)據(jù)為0,沒有數(shù)據(jù)為-1 9 10 #入棧,先判斷棧是否已經(jīng)滿了 11 def push(self,content): 12 if self.Full(): 13 print "Stack if full!" 14 else: 15 self.stack.append(content)#數(shù)據(jù)加入棧中,append()增加內(nèi)容 16 self.top=self.top+1#修改棧頂指針 17 18 #出棧 19 def out(self): 20 if self.Empty():#判斷棧是否為空 21 print "Stack is Empty!" 22 else: 23 self.top=self.top-1#棧頂指針減一 24 25 def Full(self): 26 if self.top==self.size:#棧頂指針定于棧的大小 27 #print"Stack is Empty!" 28 return True 29 else: 30 return False 31 32 def Empty(self): 33 if self.top==-1: 34 return True 35 else: 36 return False 37 def printStack(self): 38 i = self.top 39 print "amount of elements:{0}".format(self.top + 1) 40 while i >= 0: 41 print self.stack[i] 42 print "--" 43 i -= 1 44 ''' 45 st=Stack(7) 46 Stack.Empty(st) 47 Stack.push(st,"hello") 48 Stack.Empty(st) 49 Stack.Full(st) 50 Stack.push(st,"7") 51 Stack.printStack(st) 52 Stack.out(st) 53 Stack.out(st) 54 Stack.Empty(st) 55 ''' 56 st=Stack(3) 57 st.printStack() 58 st.push(1) 59 st.printStack() 60 st.push(2) 61 st.printStack() 62 st.push(3) 63 st.printStack() 64 st.push(4) 65 st.printStack() 66 st.Full() 67 #st.push(5) 68 #st.printStack() 69 70?
運行結(jié)果如圖所示:
? ? ? ? ? ??
?
運行結(jié)果解釋:
棧的空間是4,因為第一個數(shù)據(jù)是0,空間是3=1=4
push()5個才出現(xiàn)是因為4的時候沒有再次進行判斷。
Full()加上print()再執(zhí)行就有顯示棧滿。
所以棧的空間為4爾不是5.
?
三、Python 常見數(shù)據(jù)結(jié)構(gòu)-隊列
隊列在隊尾插入,隊首刪除。
代碼示例:
?
1 #coding=utf-8 2 #隊列的實現(xiàn) 3 class Queue1: 4 5 #初始化隊列 6 def __init__(self,size): 7 self.queue=[] 8 self.size=size 9 self.head=-1 10 self.tail=-1 11 12 def enQueue(self,contest): 13 if self.Full(): 14 print "Queue if full!" 15 else: 16 self.queue.append(contest) 17 self.tail=self.tail+1 18 def outQueue(self): 19 if self.Empty(): 20 print "Queue is Empty" 21 else: 22 self.head=self.head+1 23 24 def Empty(self): 25 if self.tail==self.head: 26 return True 27 else: 28 return False 29 30 def Full(self): 31 if (self.tail-self.head)==self.size: 32 print "Queue if full" 33 return True 34 else: 35 return False 36 37 qz=Queue1(3) 38 qz.enQueue(1) 39 qz.enQueue(2) 40 qz.enQueue(3) 41 qz.Empty() 42 qz.enQueue(4)?
運行結(jié)果如下圖:
? ? ? ?
?
2016-04-21 ? ?02:10:26
?
轉(zhuǎn)載于:https://www.cnblogs.com/chance88/p/5415304.html
總結(jié)
以上是生活随笔為你收集整理的python入门(七)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 团队项目-个人博客-4.20
- 下一篇: QVaraint类