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骚操作之动态定义函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Django将默认的SQLite更换为MySQL的实现

    Django将默认的SQLite更换为MySQL的实现

    今天小编就为大家分享一篇Django将默认的SQLite更换为MySQL的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • python pyqtgraph 保存图片到本地的实例

    python pyqtgraph 保存图片到本地的实例

    这篇文章主要介绍了python pyqtgraph 保存图片到本地的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Python3实现的判断回文链表算法示例

    Python3实现的判断回文链表算法示例

    这篇文章主要介绍了Python3实现的判断回文链表算法,结合实例形式分析了Python3针对链表是否为回文链表进行判断的相关算法实现技巧,需要的朋友可以参考下
    2019-03-03
  • Python发送email的3种方法

    Python发送email的3种方法

    这篇文章主要介绍了Python发送email的3种方法,本文讲解了使用登录邮件服务器方法、调用sendmail命令、使用smtp服务来发送三种方法,需要的朋友可以参考下
    2015-04-04
  • python接口调用已训练好的caffe模型测试分类方法

    python接口调用已训练好的caffe模型测试分类方法

    今天小编就为大家分享一篇python接口调用已训练好的caffe模型测试分类方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • python实现图片批量剪切示例

    python实现图片批量剪切示例

    这篇文章主要介绍了python实现图片批量剪切示例,需要的朋友可以参考下
    2014-03-03
  • Python基于docker部署的Mysql备份查询脚本

    Python基于docker部署的Mysql备份查询脚本

    这篇文章主要来和大家分享Python基于docker部署的Mysql备份查询的脚本,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起了解下
    2024-04-04
  • python Airtest自动化测试工具的的使用

    python Airtest自动化测试工具的的使用

    本文主要介绍了python Airtest自动化测试工具的的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • python读取图片颜色值并生成excel像素画的方法实例

    python读取图片颜色值并生成excel像素画的方法实例

    这篇文章主要给大家介绍了关于python读取图片颜色值并生成excel像素画的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02

最新评论