一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器
class stack(object):
def __init__(self):
self.stack = []def push(self,str1):
self.stack.append(str1)def pop(self):
return self.stack.pop()def length(self):
return len(self.stack)请问应该怎么遍历这个堆栈呢?
请看迪艾姆公司python远程视频培训班黄哥的回答
类只有实现了__iter__() 和next()方法(python3改为__next__()),生成的实例才能迭代
具有__iter__ 和next方法的对象叫迭代器
请看python2.7.5的代码
#coding:utf-8
"""
该代码由python远程培训班黄哥所写
python远程培训http://www.qy7788.com.cn/shiyongxinxi/shiyongxinxi193.html
qq:1465376564 tel:010-68165761
"""
class stack(object):
"""只有实现了__iter__和next方法的类生成的实例才可以迭代"""
def __init__(self):
self.stack = []def push(self,str1):
self.stack.append(str1)def pop(self):
return self.stack.pop()def length(self):
return len(self.stack)def __iter__(self):
return selfdef __next__(self):
try:
return self.stack.pop()
except IndexError:
raise StopIterations = stack()
s.push("python远程培训")
s.push("qq:1465376564 tel:010-68165761")
s.push("python编程思路")
for i in s:
print i请看python3.3.2的代码
#coding:utf-8
"""该代码由python远程培训班黄哥所写
python远程培训http://www.qy7788.com.cn/shiyongxinxi/shiyongxinxi193.html
qq:1465376564 tel:010-68165761 在python3.3.2环境下测试过 """
class stack(object):
"""只有实现了__iter__和next方法的类生成的实例才可以迭代"""
def __init__(self):
self.stack = []
def push(self,str1):
self.stack.append(str1)
def pop(self):
return self.stack.pop()
def length(self):
return len(self.stack)
def __iter__(self):
return self
def __next__(self):
try:
return self.stack.pop()
except IndexError:
raise StopIteration
s = stack()
s.push("python远程培训")
s.push("qq:1465376564 tel:010-68165761")
s.push("python编程思路")
for item in s:
print(item)
一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网的更多相关文章
- 从零开始写STL—栈和队列
从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...
- springmvc2 一个控制器写多个方法(非注解方式)
出处:http://blog.csdn.net/xuewenke/article/details/23895999 springmvc2 一个控制器写多个方法(非注解方式) 分类: spring 20 ...
- SpringMVC实现一个controller写多个方法
MultiActionController与ParameterMethodNameResolver在一个Controller类中定义多个方法,并根据使用者的请求来执行当中的某个方法,相当于Struts ...
- PHP的排列组合问题 分别从每一个集合中取出一个元素进行组合,问有多少种组合?
首先说明这是一个数学的排列组合问题C(m,n) = m!/(n!*(m-n)!) 比如:有集合('粉色','红色','蓝色','黑色'),('38码','39码','40码'),('大号','中号') ...
- 设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称
转载请注明出处:http://blog.csdn.net/u012860063 问题:设单链表中存放n个字符.试设计一个算法,使用栈推断该字符串是否中心对称,如xyzzyx即为中心对称字符串. 代码例 ...
- 分享一个c#写的开源分布式消息队列equeue
分享一个c#写的开源分布式消息队列equeue 前言 equeue消息队列中的专业术语 Topic Queue Producer Consumer Consumer Group Broker 集群消费 ...
- 分享一个自己写的MVC+EF “增删改查” 无刷新分页程序
分享一个自己写的MVC+EF “增删改查” 无刷新分页程序 一.项目之前得添加几个组件artDialog.MVCPager.kindeditor-4.0.先上几个效果图. 1.首先建立一个数 ...
- EQueue - 一个C#写的开源分布式消息队列的总体介绍(转)
源: EQueue - 一个C#写的开源分布式消息队列的总体介绍 EQueue - 一个纯C#写的分布式消息队列介绍2 EQueue - 详细谈一下消息持久化以及消息堆积的设计
- vue element-ui怎样提炼一个自己写的js当作公共js
vue element-ui怎样提炼一个自己写的js当作公共js请教一下各位大神,我刚刚触摸vue element-ui几天,写的一个清晰检索的input框,现在需当作项目公共的部分,可遭需的html ...
随机推荐
- iOS图片拉伸技巧-李明杰分享
http://bbs.itcast.cn/thread-21436-1-1.html 本文目录 "一.iOS5.0之前------------------------------------ ...
- 高级UIKit-09(TCPSocket发送文件、上传和下载)
[day1101_SocketSendFile]:发送文件到服务端 发送文件需要在该文件上拼接消息头,比如类型,文件名,文件大小 // 服务端 - (void)viewDidLoad { [super ...
- 用overflow-y 解决web页面抖动问题
页面抖动(左右抖动)让人视觉上很不爽.. /** original : php攻城师 http://blog.csdn.net/phpgcs **/ 最开始我也以为是 layout 不一致的原因..后 ...
- WPF(布局)
WPF编程学习——布局 本文目录 1.布局简介 2.面板(Panel) 3.视图框(Viewbox) 4.滚动视图控件(ScrollViewer) 5.公共布局属性 1.布局简介 应用程序界面 ...
- [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面
cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...
- webBrowser中操作网页元素全攻略
原文 webBrowser中操作网页元素全攻略 1.获取非input控件的值: webBrowser1.Document.All["控件ID"].InnerText; 或webBr ...
- 获取wpf datagrid当前被编辑单元格的内容
原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...
- 73_leetcode_Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree 1:中序和后序遍历构成一棵树.2:採用递归的方法. ...
- hdu 1421 搬寝室 (dp)
思路分析: dp[i][j] 表示选取到第 i 个 组成了 j 对的最优答案. 当然排序之后 选取相邻两个是更优的. if(i==j*2) dp[i][j] = dp[i-2][j-1] + w[ ...
- sql:oracle, CURSOR
CursorsYou use a cursor to fetch rows returned by a query. You retrieve the rows into the cursor usi ...