美文网首页
(九)迭代

(九)迭代

作者: 费云帆 | 来源:发表于2019-01-02 10:42 被阅读0次

1.几个名词

  • 循环(loop),指的是在满足条件的情况下,重复执行同一段代码。比如,while语句。
  • 迭代(iterate),指的是按照某种顺序逐个访问列表中的每一项。比如,for语句。
  • 递归(recursion),指的是一个函数不断调用自身的行为。比如,以编程方式输出著名的斐波纳契数列。
  • 遍历(traversal),指的是按照一定的规则访问树形结构中的每个节点,而且每个节点都只访问一次。

2.迭代的基本操作,除了for,还可以使用iter()

>>> list1=['q','i','w','s','i','r']
# 使用iter()创建一个迭代器
>>> list2=iter(list1)
# 使用.__next__()一直迭代下去
>>> list2.__next__()
'q'
>>> list2.__next__()
'i'
>>> list2.__next__()
'w'
>>> list2.__next__()
's'
>>> list2.__next__()
'i'
>>> list2.__next__()
'r'
# 迭代到最后会报错...因为末尾了
# 使用for不会报错
>>> list2.__next__()
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    list2.__next__()
StopIteration
# 懒惰一点的办法
# 需要重新赋值,因为已经迭代到末尾了
# 这也是迭代器的特点,要小心指针的位置
>>> list3=iter(list1)
>>> while True:
    print(list3.__next__())
q
i
w
s
i
r
Traceback (most recent call last):
  File "<pyshell#13>", line 2, in <module>
    print(list3.__next__())
StopIteration
  • 文件的迭代:之前的操作,使用readline()或者for()去遍历,这里使用next(),for的本质就是next():
>>> file=open('test3.txt')
# 文件对象不需要创建iter(),直接可以用
>>> file.__next__()
'Learn python with qiwsir.\n'
>>> file.__next__()
'There is free python course.\n'
>>> file.__next__()
'http://qiwsir.github.io\n'
>>> file.__next__()
'Its language is Chinese.'
>>> file.__next__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
  • 还可以使用list(),tuple(),''.join()获取文件元素
>>> file=open('test3.txt')
>>> list(file)
['Learn python with qiwsir.\n', 'There is free python course.\n', 'http://qiwsir
.github.io\n', 'Its language is Chinese.']
# 需要重新赋值
>>> tuple(file)
()
>>> file=open('test3.txt')
>>> tuple(file)
('Learn python with qiwsir.\n', 'There is free python course.\n', 'http://qiwsir
.github.io\n', 'Its language is Chinese.')
>>> file=open('test3.txt')
>>> '$$$'.join(file)
'Learn python with qiwsir.\n$$$There is free python course.\n$$$http://qiwsir.gi
thub.io\n$$$Its language is Chinese.'
>>>
# 亮瞎眼的操作
>>> file=open('test3.txt')
# a,b,c,d,e=file 会报错,file有四个元素,赋值5个报错
# 赋值的元素要刚好.
>>> a,b,c,d=file
>>> a
'Learn python with qiwsir.\n'
>>> b
'There is free python course.\n'
>>> c
'http://qiwsir.github.io\n'
>>> d
'Its language is Chinese.'

相关文章

  • (九)迭代

    1.几个名词 循环(loop),指的是在满足条件的情况下,重复执行同一段代码。比如,while语句。 迭代(ite...

  • 设计模式学习专栏九--------迭代器模式

    设计模式学习专栏九--------迭代器模式 名称 : 迭代器模式(Iterator) 价值观念: 管理良好的集合...

  • (九)迭代与测试

    迭代 两种基本的迭代方式 功能驱动开发焦点是功能列表,一个一个功能 用例驱动开发焦点是用例图,一个一个场景 Gra...

  • php设计模式(九)迭代器

    迭代器模式 不了解内部实现前提下,遍历一个对象

  • 《商业模式新生代》读后感

    书本内容 本书通过九大模块,五种商业模式类型,讲述了企业模式的迭代更换和成功史。 主体结构 九大模块分别是: * ...

  • 迭代

    经济发展快 时代在迭代 不想被淘汰 必须去迭代 认知需迭代 思维需迭代 行为需迭代 迭代跟潮流 迭代战自我 迭代向未来

  • stream系列——初识stream

    初识stream 1、对于迭代来说,包含内部迭代和外部迭代。 外部迭代:(程序逻辑自己控制迭代过程) 内部迭代:(...

  • 迭代器与生成器

    迭代(iteration)与可迭代(iterable) 迭代器 迭代器协议(iterator protocol) ...

  • 迭代器,生成器,面向过程编程

    迭代器 一、迭代的概念迭代器即迭代的工具,那什么是迭代呢?迭代是一个重复的过程。每一次重复即一次迭代,并且每次迭代...

  • 第7章迭代器模式

    第7章迭代器模式 7.1 jQuery中的迭代器 7.2 实现自己的迭代器 7.3 内部迭代和外部迭代 1内部迭代...

网友评论

      本文标题:(九)迭代

      本文链接:https://www.haomeiwen.com/subject/wpnalqtx.html