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开发chrome插件的方法分析

    基于Python开发chrome插件的方法分析

    这篇文章主要介绍了基于Python开发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开发.exe小工具的详细步骤

    Python开发.exe小工具的详细步骤

    这篇文章主要介绍了Python开发.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

最新评论