# 迭代

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

## 逐个访问

```>>> lst
['q', 'i', 'w', 's', 'i', 'r']
>>> for i in lst:
...     print i,
...
q i w s i r```

```>>> lst_iter = iter(lst)    #对原来的 list 实施了一个 iter()
>>> lst_iter.next()         #要不厌其烦地一个一个手动访问
'q'
>>> lst_iter.next()
'i'
>>> lst_iter.next()
'w'
>>> lst_iter.next()
's'
>>> lst_iter.next()
'i'
>>> lst_iter.next()
'r'
>>> lst_iter.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration```

`iter()`是一个内建函数，其含义是：

```>>> while True:
...     print lst_iter.next()
...
Traceback (most recent call last):      #居然报错，而且错误跟前面一样？什么原因
File "<stdin>", line 2, in <module>
StopIteration```

```>>> lst_iter = iter(lst)                #上面的错误暂且搁置，回头在研究
>>> while True:
...     print lst_iter.next()
...
q                                       #果然自动化地读取了
i
w
s
i
r
Traceback (most recent call last):      #读取到最后一个之后，报错，停止循环
File "<stdin>", line 2, in <module>
StopIteration```

iter(o[, sentinel])

Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the iter() method), or it must support the sequence protocol (the getitem() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then o must be a callable object. The iterator created in this case will call o with no arguments for each call to its next() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

• 返回值是一个迭代器对象
• 参数需要是一个符合迭代协议的对象或者是一个序列对象
• next() 配合与之使用

## 文件迭代器

```Learn python with qiwsir.
There is free python course.
The website is:
http://qiwsir.github.io
Its language is Chinese.```

```>>> f = open("208.txt")
'Learn python with qiwsir.\n'
'There is free python course.\n'
'The website is:\n'
'http://qiwsir.github.io\n'
'Its language is Chinese.\n'
''```

```>>> for line in f:     #这个操作是紧接着上面的操作进行的，请看官主要观察
...     print line,    #没有打印出任何东西
...```

```>>> f = open("208.txt")     #从头再来
>>> for line in f:
...     print line,
...
Learn python with qiwsir.
There is free python course.
The website is:
http://qiwsir.github.io
Its language is Chinese.```

```>>> f = open("208.txt")
>>> f.next()
'Learn python with qiwsir.\n'
>>> f.next()
'There is free python course.\n'
>>> f.next()
'The website is:\n'
>>> f.next()
'http://qiwsir.github.io\n'
>>> f.next()
'Its language is Chinese.\n'
>>> f.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration```

```>>> [ line for line in open('208.txt') ]
['Learn python with qiwsir.\n', 'There is free python course.\n', 'The website is:\n', 'http://qiwsir.github.io\n', 'Its language is Chinese.\n']```

```>>> list(open('208.txt'))
['Learn python with qiwsir.\n', 'There is free python course.\n', 'The website is:\n', 'http://qiwsir.github.io\n', 'Its language is Chinese.\n']

>>> tuple(open('208.txt'))
('Learn python with qiwsir.\n', 'There is free python course.\n', 'The website is:\n', 'http://qiwsir.github.io\n', 'Its language is Chinese.\n')

>>> "\$\$\$".join(open('208.txt'))
'Learn python with qiwsir.\n\$\$\$There is free python course.\n\$\$\$The website is:\n\$\$\$http://qiwsir.github.io\n\$\$\$Its language is Chinese.\n'

>>> a,b,c,d,e = open("208.txt")
>>> a
'Learn python with qiwsir.\n'
>>> b
'There is free python course.\n'
>>> c
'The website is:\n'
>>> d
'http://qiwsir.github.io\n'
>>> e
'Its language is Chinese.\n'```