python列表操作实例


Posted in Python onJanuary 14, 2015

本文实例讲述了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操作MySQL数据库的方法分享
May 29 Python
Python使用scrapy采集数据时为每个请求随机分配user-agent的方法
Apr 08 Python
Python MySQLdb模块连接操作mysql数据库实例
Apr 08 Python
python函数形参用法实例分析
Aug 04 Python
windows环境下tensorflow安装过程详解
Mar 30 Python
对numpy 数组和矩阵的乘法的进一步理解
Apr 04 Python
Python实现图片拼接的代码
Jul 02 Python
对Python强大的可变参数传递机制详解
Jun 13 Python
python实现简单五子棋游戏
Jun 18 Python
Python3+Appium实现多台移动设备操作的方法
Jul 05 Python
python requests使用socks5的例子
Jul 25 Python
Python中断多重循环的思路总结
Oct 04 Python
python操作gmail实例
Jan 14 #Python
Python中的装饰器用法详解
Jan 14 #Python
python登陆asp网站页面的实现代码
Jan 14 #Python
Python的面向对象思想分析
Jan 14 #Python
为python设置socket代理的方法
Jan 14 #Python
Python单例模式实例分析
Jan 14 #Python
python文件读写操作与linux shell变量命令交互执行的方法
Jan 14 #Python
You might like
解析yii数据库的增删查改
2013/06/20 PHP
ThinkPHP实现递归无级分类――代码少
2015/07/29 PHP
关于laravel 子查询 & join的使用
2019/10/16 PHP
PHP多进程简单实例小结
2019/11/09 PHP
图片上传插件jquery.uploadify详解
2013/11/15 Javascript
Jquery中request和request.form和request.querystring的区别
2015/11/26 Javascript
jquery easyui DataGrid简单示例
2017/01/23 Javascript
JS使用tween.js动画库实现轮播图并且有切换功能
2018/07/17 Javascript
浅谈vue异步数据影响页面渲染
2019/10/29 Javascript
jQuery实现html可联动的百分比进度条
2020/03/26 jQuery
python实现梯度下降算法
2020/03/24 Python
python try 异常处理(史上最全)
2019/03/07 Python
python3使用matplotlib绘制散点图
2019/03/19 Python
Django组件cookie与session的具体使用
2019/06/05 Python
华为校园招聘上机笔试题 扑克牌大小(python)
2020/04/22 Python
Python实现的企业粉丝抽奖功能示例
2019/07/26 Python
对Django 中request.get和request.post的区别详解
2019/08/12 Python
pytorch之ImageFolder使用详解
2020/01/06 Python
python操作docx写入内容,并控制文本的字体颜色
2020/02/13 Python
Python 中由 yield 实现异步操作
2020/05/04 Python
Python远程方法调用实现过程解析
2020/07/28 Python
HTML5+CSS3 诱人的实例:3D立方体旋转动画实例
2016/12/30 HTML / CSS
英国办公家具网站:Furniture At Work
2019/10/07 全球购物
中式婚礼主持词
2014/03/13 职场文书
小学生安全演讲稿
2014/04/25 职场文书
企业负责人任命书
2014/06/05 职场文书
2014年学雷锋活动总结
2014/06/26 职场文书
模具设计与制造专业自荐书
2014/07/01 职场文书
中秋节活动总结
2014/08/29 职场文书
感恩祖国演讲稿
2014/09/09 职场文书
2015年工商所工作总结
2015/05/21 职场文书
金榜题名主持词
2015/07/02 职场文书
源码解读Spring-Integration执行过程
2021/06/11 Java/Android
java实现对Hadoop的操作
2021/07/01 Java/Android
MySQL系列之五 视图、存储函数、存储过程、触发器
2021/07/02 MySQL
Python  lambda匿名函数和三元运算符
2022/04/19 Python