• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python列表操作实例

python列表操作实例

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python脚本编写实例,python项目实例,python开发实例,python开发网站实例,python web开发实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python列表操作的方法。分享给大家供大家参考。

具体实现方法如下:

   """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</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python脚本爬取字体文件的实现方法
  • Python只用40行代码编写的计算器实例
  • Python实现脚本锁功能(同时只能执行一个脚本)
  • python脚本爬取字体文件的实现方法
  • python获取当前运行函数名称的方法实例代码
  • Python类的动态修改的实例方法
  • Python中动态创建类实例的方法
  • Python自动发邮件脚本
  • Python类的动态修改的实例方法
  • Python中动态创建类实例的方法

相关文章

  • Python实现类似jQuery使用中的链式调用的示例
  • python通过线程实现定时器timer的方法
  • python文件和目录操作函数小结
  • 跟老齐学Python之复习if语句
  • Python脚本实现格式化css文件
  • 将Python代码打包为jar软件的简单方法
  • 深入理解python try异常处理机制
  • Linux下通过python访问MySQL、Oracle、SQL Server数据库的方法
  • 详解常用查找数据结构及算法(Python实现)
  • Python中操作文件之write()方法的使用教程

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python交换变量
    • python中sys.argv参数用法实例分析
    • 使用Python的Twisted框架编写简单的网络客户端
    • Python实现的ini文件操作类分享
    • python实现模拟按键,自动翻页看u17漫画
    • Python运算符重载用法实例分析
    • python使用rabbitmq实现网络爬虫示例
    • 详解Python的Twisted框架中reactor事件管理器的用法
    • python生成随机密码或随机字符串的方法
    • Python读取环境变量的方法和自定义类分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有