python列表操作實(shí)例
本文實(shí)例講述了python列表操作的方法。分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
"""Single node in a data structure"""
def __init__(self, data):
"""Node constructor"""
self._data = data
self._nextNode = None
def __str__(self):
"""Node data representation"""
return str(self._data)
class List:
"""Linked list"""
def __init__(self):
"""List constructor"""
self._firstNode = None
self._lastNode = None
def __str__(self):
"""List string representation"""
if self.isEmpty():
return "empty"
currentNode = self._firstNode
output = []
while currentNode is not None:
output.append(str(currentNode._data))
currentNode = currentNode._nextNode
return " ".join(output)
def insertAtFront(self, value):
"""Insert node at front of list"""
newNode = Node(value)
if self.isEmpty(): # List is empty
self._firstNode = self._lastNode = newNode
else: # List is not empty
newNode._nextNode = self._firstNode
self._firstNode = newNode
def insertAtBack(self, value):
"""Insert node at back of list"""
newNode = Node(value)
if self.isEmpty(): # List is empty
self._firstNode = self._lastNode = newNode
else: # List is not empty
self._lastNode._nextNode = newNode
self._lastNode = newNode
def removeFromFront(self):
"""Delete node from front of list"""
if self.isEmpty(): # raise exception on empty list
raise IndexError, "remove from empty list"
tempNode = self._firstNode
if self._firstNode is self._lastNode: # one node in list
self._firstNode = self._lastNode = None
else:
self._firstNode = self._firstNode._nextNode
return tempNode
def removeFromBack(self):
"""Delete node from back of list"""
if self.isEmpty(): # raise exception on empty list
raise IndexError, "remove from empty list"
tempNode = self._lastNode
if self._firstNode is self._lastNode: # one node in list
self._firstNode = self._lastNode = None
else:
currentNode = self._firstNode
# locate second-to-last node
while currentNode._nextNode is not self._lastNode:
currentNode = currentNode._nextNode
currentNode._nextNode = None
self._lastNode = currentNode
return tempNode
def isEmpty(self):
"""Returns true if List is empty"""
return self._firstNode is None
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python騷操作之動(dòng)態(tài)定義函數(shù)
這篇文章主要介紹了Python騷操作之動(dòng)態(tài)定義函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Django將默認(rèn)的SQLite更換為MySQL的實(shí)現(xiàn)
今天小編就為大家分享一篇Django將默認(rèn)的SQLite更換為MySQL的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
python pyqtgraph 保存圖片到本地的實(shí)例
這篇文章主要介紹了python pyqtgraph 保存圖片到本地的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Python3實(shí)現(xiàn)的判斷回文鏈表算法示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的判斷回文鏈表算法,結(jié)合實(shí)例形式分析了Python3針對(duì)鏈表是否為回文鏈表進(jìn)行判斷的相關(guān)算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03
python接口調(diào)用已訓(xùn)練好的caffe模型測(cè)試分類方法
今天小編就為大家分享一篇python接口調(diào)用已訓(xùn)練好的caffe模型測(cè)試分類方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
python Airtest自動(dòng)化測(cè)試工具的的使用
本文主要介紹了python Airtest自動(dòng)化測(cè)試工具的的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
python讀取圖片顏色值并生成excel像素畫(huà)的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于python讀取圖片顏色值并生成excel像素畫(huà)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

