亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python列表操作實(shí)例

 更新時(shí)間:2015年01月14日 14:30:56   投稿:shichen2014  
這篇文章主要介紹了python列表操作方法,實(shí)例分析了Python針對(duì)列表操作的插入、刪除等各種操作技巧,需要的朋友可以參考下

本文實(shí)例講述了python列表操作的方法。分享給大家供大家參考。

具體實(shí)現(xiàn)方法如下:

復(fù)制代碼 代碼如下:
class Node:
   """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 給某個(gè)文件名添加時(shí)間戳的方法

    Python 給某個(gè)文件名添加時(shí)間戳的方法

    今天小編就為大家分享一篇Python 給某個(gè)文件名添加時(shí)間戳的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Python中函數(shù)的用法實(shí)例教程

    Python中函數(shù)的用法實(shí)例教程

    這篇文章主要介紹了Python中函數(shù)的用法,以數(shù)值計(jì)算的實(shí)例方式講述了Python程序設(shè)計(jì)中函數(shù)的功能機(jī)抽象化特點(diǎn),需要的朋友可以參考下
    2014-09-09
  • 基于Python開發(fā)chrome插件的方法分析

    基于Python開發(fā)chrome插件的方法分析

    這篇文章主要介紹了基于Python開發(fā)chrome插件的方法,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)chrome瀏覽器插件相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • Python使用pycharm導(dǎo)入pymysql教程

    Python使用pycharm導(dǎo)入pymysql教程

    這篇文章主要介紹了Python使用pycharm導(dǎo)入pymysql教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • flask框架路由常用定義方式總結(jié)

    flask框架路由常用定義方式總結(jié)

    這篇文章主要介紹了flask框架路由常用定義方式,結(jié)合實(shí)例形式總結(jié)分析了flask框架路由的常見(jiàn)定義方式與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-07-07
  • Python開發(fā).exe小工具的詳細(xì)步驟

    Python開發(fā).exe小工具的詳細(xì)步驟

    這篇文章主要介紹了Python開發(fā).exe小工具的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • python障礙式期權(quán)定價(jià)公式

    python障礙式期權(quán)定價(jià)公式

    這篇文章主要為大家詳細(xì)介紹了python障礙式期權(quán)定價(jià)公式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Python數(shù)據(jù)分析庫(kù)pandas高級(jí)接口dt的使用詳解

    Python數(shù)據(jù)分析庫(kù)pandas高級(jí)接口dt的使用詳解

    這篇文章主要介紹了Python數(shù)據(jù)分析庫(kù)pandas高級(jí)接口dt的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Python實(shí)現(xiàn)簡(jiǎn)單遺傳算法(SGA)

    Python實(shí)現(xiàn)簡(jiǎn)單遺傳算法(SGA)

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡(jiǎn)單遺傳算法SGA,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • python 列表輸出重復(fù)值以及對(duì)應(yīng)的角標(biāo)方法

    python 列表輸出重復(fù)值以及對(duì)應(yīng)的角標(biāo)方法

    今天小編就為大家分享一篇python 列表輸出重復(fù)值以及對(duì)應(yīng)的角標(biāo)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06

最新評(píng)論