python列表操作实例

yipeiwu_com6年前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处理XML格式数据的方法详解

本文实例讲述了Python处理XML格式数据的方法。分享给大家供大家参考,具体如下: 这里的操作是基于Python3平台。 在使用Python处理XML的问题上,首先遇到的是编码问题。...

解决Django加载静态资源失败的问题

解决Django加载静态资源失败的问题

Django项目中为什么会加载静态时会失败呢? 原因:django部署方式比较特别,采用静态文件路径:STATICFILES_DIRS的部署方式,之前你写的相对路径,绝对路径因为缺少静态...

Python 中导入csv数据的三种方法

Python 中导入csv数据的三种方法,具体内容如下所示: 1、通过标准的Python库导入CSV文件: Python提供了一个标准的类库CSV文件。这个类库中的reader()函数用...

对Django外键关系的描述

注:本文需要你有一定的数据库知识,本文的数据库语法使用mysql书写 Django中,跟外键有关的关系有三种,下面来一一介绍。 OneToManyField 这种最好理解,说白了就是最普...