欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python列表操作實例

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

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

具體實現方法如下:

復制代碼 代碼如下:
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

希望本文所述對大家的Python程序設計有所幫助。

相關文章

  • Python 給某個文件名添加時間戳的方法

    Python 給某個文件名添加時間戳的方法

    今天小編就為大家分享一篇Python 給某個文件名添加時間戳的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python中函數的用法實例教程

    Python中函數的用法實例教程

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

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

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

    Python使用pycharm導入pymysql教程

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

    flask框架路由常用定義方式總結

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

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

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

    python障礙式期權定價公式

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

    Python數據分析庫pandas高級接口dt的使用詳解

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

    Python實現簡單遺傳算法(SGA)

    這篇文章主要為大家詳細介紹了Python實現簡單遺傳算法SGA,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • python 列表輸出重復值以及對應的角標方法

    python 列表輸出重復值以及對應的角標方法

    今天小編就為大家分享一篇python 列表輸出重復值以及對應的角標方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06

最新評論