棧如何在python項(xiàng)目中實(shí)現(xiàn)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)棧如何在python項(xiàng)目中實(shí)現(xiàn),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

成都創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為陸良等服務(wù)建站,陸良等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為陸良企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

棧是一種線性數(shù)據(jù)結(jié)構(gòu),用先進(jìn)后出或者是后進(jìn)先出的方式存儲(chǔ)數(shù)據(jù),棧中數(shù)據(jù)的插入刪除操作都是在棧頂端進(jìn)行,常見棧的函數(shù)操作包括

  • empty() – 返回棧是否為空 – Time Complexity : O(1)

  • size() – 返回棧的長(zhǎng)度 – Time Complexity : O(1)

  • top() – 查看棧頂元素 – Time Complexity : O(1)

  • push(g) – 向棧頂添加元素 – Time Complexity : O(1)

  • pop() – 刪除棧頂元素 – Time Complexity : O(1)

python中棧可以用以下三種方法實(shí)現(xiàn):

1)list

2)collections.deque

3)queue.LifoQueue

使用列表實(shí)現(xiàn)棧

python的內(nèi)置數(shù)據(jù)結(jié)構(gòu)list可以用來(lái)實(shí)現(xiàn)棧,用append()向棧頂添加元素, pop() 可以以后進(jìn)先出的順序刪除元素

但是列表本身有一些缺點(diǎn),主要問(wèn)題就是當(dāng)列表不斷擴(kuò)大的時(shí)候會(huì)遇到速度瓶頸.列表是動(dòng)態(tài)數(shù)組,因此往其中添加新元素而沒有空間保存新的元素時(shí),它會(huì)自動(dòng)重新分配內(nèi)存塊,并將原來(lái)的內(nèi)存中的值復(fù)制到新的內(nèi)存塊中.這就導(dǎo)致了一些append()操作會(huì)消耗更多的時(shí)間

>>> stack = []
>>> #append() fuction to push
... #element in list
... 
>>> stack.append('hello')
>>> stack.append('world')
>>> stack.append('!')
>>> print('Initial stack')
Initial stack
>>> print(stack)
['hello', 'world', '!']
>>> #pop() function to pop element
... #from stack in LIFO order
... 
>>> print('\nElement poped from stack')

Element poped from stack

>>> print(stack.pop())
!
>>> print(stack.pop())
world
>>> print(stack.pop())
hello
>>> print('\nStack after all elements are poped')

Stack after all elements are poped
>>> print(stack)
[]

使用collections.deque實(shí)現(xiàn)棧

python中棧也可以用deque類實(shí)現(xiàn),當(dāng)我們想要在實(shí)現(xiàn)在容器兩端更快速地進(jìn)行append和pop操作時(shí),deque比列表更合適.deque可以提供O(1)時(shí)間的append和pop操作,而列表則需要O(n)時(shí)間.

>>> from collections import deque
>>> stack = deque()
>>> # append() fuction to push
... #element in list
... 
>>> stack.append('hello')
>>> stack.append('world')
>>> stack.append('!')
>>> print('Initial stack')
Initial stack
>>> print(stack)
deque(['hello', 'world', '!'])
>>> #pop() function to pop element
... #from stack in LIFO order
... 
>>> print('\nElement poped from stack')

Element poped from stack
>>> print(stack.pop())
!
>>> print(stack.pop())
world
>>> print(stack.pop())
hello
>>> print('\nStack after all elements are poped')

Stack after all elements are poped
>>> print(stack)deque([])

使用queue module實(shí)現(xiàn)棧

Queue模塊有LIFO queue,也就是棧結(jié)構(gòu).用put()和get()操作從Queue中添加和獲得數(shù)據(jù)

>>> from queue import LifoQueue
>>> stack = LifoQueue(maxsize = 3)
>>> print(stack.qsize())
0
>>> stack.put('hello')
>>> stack.put('world')
>>> stack.put('!')
>>> print('\nElement poped from stack')

Element poped from stack
>>> print(stack.get())
!
>>> print(stack.get())
world
>>> print(stack.get())
hello
>>> print('\nEmpty:', stack.empty())

Empty: True

關(guān)于棧如何在python項(xiàng)目中實(shí)現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

本文標(biāo)題:棧如何在python項(xiàng)目中實(shí)現(xiàn)-創(chuàng)新互聯(lián)
文章鏈接:http://muchs.cn/article26/cdiecg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站域名注冊(cè)、動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)