Skip to content

Commit 1ddc92a

Browse files
committed
Signed-off-by: twowater <[email protected]>
草根学Python(七) 迭代器和生成器
1 parent 2dbc653 commit 1ddc92a

File tree

8 files changed

+523
-1
lines changed

8 files changed

+523
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
|草根学 Python(三)List 和 Tuple|[掘金](https://juejin.im/post/593fdb87128fe1006a02ce92)[简书](http://www.jianshu.com/p/97c97d5a5a7c)[CSDN](http://blog.csdn.net/Two_Water/article/details/73524367)[个人博客](http://twowater.com.cn/2017/06/21/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%B8%89-List-%E5%92%8C-Tuple/)|
1414
|草根学Python(四) Dict 和 Set|[掘金](https://juejin.im/post/5947bf84ac502e5490e4a6a1)[简书](http://www.jianshu.com/p/90f5b897ce77)[CSDN](http://blog.csdn.net/two_water/article/details/73719026)[个人博客](http://twowater.com.cn/2017/06/25/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E5%9B%9B-Dict-%E5%92%8C-Set/)|
1515
|草根学Python(五) 条件语句和循环语句|[掘金](https://juejin.im/post/594c6c52f265da6c1f75f164)[简书](http://www.jianshu.com/p/2b80009b1e8c)[CSDN](http://blog.csdn.net/Two_Water/article/details/73762517)[个人博客](http://twowater.com.cn/2017/06/27/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%BA%94-%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5%E5%92%8C%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5/)|
16-
|草根学Python(六) 函数|[掘金](https://juejin.im/post/5946784461ff4b006cf1d8ec)[简书](http://www.jianshu.com/p/d8f2a55edc75)[CSDN](http://blog.csdn.net/Two_Water/article/details/73865622)[个人博客](http://twowater.com.cn/2017/06/29/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E5%85%AD-%E5%87%BD%E6%95%B0/)|
16+
|草根学Python(六) 函数|[掘金](https://juejin.im/post/5946784461ff4b006cf1d8ec)[简书](http://www.jianshu.com/p/d8f2a55edc75)[CSDN](http://blog.csdn.net/Two_Water/article/details/73865622)[个人博客](http://twowater.com.cn/2017/06/29/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E5%85%AD-%E5%87%BD%E6%95%B0/)|
17+
|草根学Python(七) 迭代器和生成器|[掘金](https://juejin.im/post/59589fedf265da6c386ce4ac)[简书](http://www.jianshu.com/p/74c0c1db1490)[CSDN](http://blog.csdn.net/Two_Water/article/details/74164652)[个人博客](http://twowater.com.cn/2017/07/02/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%B8%83-%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%92%8C%E7%94%9F%E6%88%90%E5%99%A8/)|

SUMMARY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@
3131
* [三、函数返回值](/python6/3.md)
3232
* [四、函数的参数](/python6/4.md)
3333
* [五、匿名函数](/python6/5.md)
34+
* [迭代器和生成器](/python7/Preface.md)
35+
* [一、迭代](/python7/1.md)
36+
* [二、Python 迭代器](/python7/2.md)
37+
* [三、lsit 生成式(列表生成式)](/python7/3.md)
38+
* [四、生成器](/python7/4.md)
39+
* [五、迭代器和生成器综合例子](/python7/5.md)

python7/1.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 一、迭代 #
2+
3+
什么叫做迭代?
4+
5+
比如在 Java 中,我们通过 List 集合的下标来遍历 List 集合中的元素,在 Python 中,给定一个 list 或 tuple,我们可以通过 for 循环来遍历这个 list 或 tuple ,这种遍历就是迭代。
6+
7+
可是,Python 的 `for` 循环抽象程度要高于 Java 的 `for` 循环的,为什么这么说呢?因为 Python 的 `for` 循环不仅可以用在 list 或tuple 上,还可以作用在其他可迭代对象上。也就是说,只要是可迭代的对象,无论有没有下标,都是可以迭代的。
8+
9+
比如:
10+
11+
```python
12+
13+
# -*- coding: UTF-8 -*-
14+
15+
# 1、for 循环迭代字符串
16+
for char in 'liangdianshui' :
17+
print ( char , end = ' ' )
18+
19+
print('\n')
20+
21+
# 2、for 循环迭代 list
22+
list1 = [1,2,3,4,5]
23+
for num1 in list1 :
24+
print ( num1 , end = ' ' )
25+
26+
print('\n')
27+
28+
# 3、for 循环也可以迭代 dict (字典)
29+
dict1 = {'name':'两点水','age':'23','sex':''}
30+
31+
for key in dict1 : # 迭代 dict 中的 key
32+
print ( key , end = ' ' )
33+
34+
print('\n')
35+
36+
for value in dict1.values() : # 迭代 dict 中的 value
37+
print ( value , end = ' ' )
38+
39+
print ('\n')
40+
41+
# 如果 list 里面一个元素有两个变量,也是很容易迭代的
42+
for x , y in [ (1,'a') , (2,'b') , (3,'c') ] :
43+
print ( x , y )
44+
45+
```
46+
47+
输出的结果如下:
48+
49+
```txt
50+
l i a n g d i a n s h u i
51+
52+
1 2 3 4 5
53+
54+
name age sex
55+
56+
两点水 23 男
57+
58+
1 a
59+
2 b
60+
3 c
61+
```

python7/2.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 二、Python 迭代器 #
2+
3+
上面简单的介绍了一下迭代,迭代是 Python 最强大的功能之一,是访问集合元素的一种方式。现在正式进入主题:迭代器,迭代器是一个可以记住遍历的位置的对象。
4+
5+
迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。
6+
7+
迭代器只能往前不会后退。
8+
9+
迭代器有两个基本的方法:iter() 和 next(),且字符串,列表或元组对象都可用于创建迭代器,迭代器对象可以使用常规 for 语句进行遍历,也可以使用 next() 函数来遍历。
10+
11+
具体的实例:
12+
13+
```python
14+
# 1、字符创创建迭代器对象
15+
str1 = 'liangdianshui'
16+
iter1 = iter ( str1 )
17+
18+
# 2、list对象创建迭代器
19+
list1 = [1,2,3,4]
20+
iter2 = iter ( list1 )
21+
22+
# 3、tuple(元祖) 对象创建迭代器
23+
tuple1 = ( 1,2,3,4 )
24+
iter3 = iter ( tuple1 )
25+
26+
# for 循环遍历迭代器对象
27+
for x in iter1 :
28+
print ( x , end = ' ' )
29+
30+
print('\n------------------------')
31+
32+
# next() 函数遍历迭代器
33+
while True :
34+
try :
35+
print ( next ( iter3 ) )
36+
except StopIteration :
37+
break
38+
39+
```
40+
41+
最后输出的结果:
42+
43+
```txt
44+
l i a n g d i a n s h u i
45+
------------------------
46+
1
47+
2
48+
3
49+
4
50+
```

python7/3.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 三、lsit 生成式(列表生成式) #
2+
3+
4+
## 1、创建 list 的方式 ##
5+
6+
之前经过我们的学习,都知道如何创建一个 list ,可是有些情况,用赋值的形式创建一个 list 太麻烦了,特别是有规律的 list ,一个一个的写,一个一个赋值,太麻烦了。比如要生成一个有 30 个元素的 list ,里面的元素为 1 - 30 。我们可以这样写:
7+
8+
```python
9+
# -*- coding: UTF-8 -*-
10+
11+
list1=list ( range (1,31) )
12+
print(list1)
13+
```
14+
15+
输出的结果:
16+
17+
```txt
18+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
19+
```
20+
21+
这个其实在之前也有提到过:比如有个例子,打印九九乘法表,用这个方法其实就几句代码就可以了,具体可以看之前的这个章节:[条件语句和循环语句综合实例](https://www.readwithu.com/python5/Example.html)
22+
23+
但是,如果用到 list 生成式,可以一句代码就生成九九乘法表了。具体看代码:
24+
25+
```python
26+
print('\n'.join([' '.join ('%dx%d=%2d' % (x,y,x*y) for x in range(1,y+1)) for y in range(1,10)]))
27+
```
28+
29+
最后输出的结果:
30+
31+
```txt
32+
1x1= 1
33+
1x2= 2 2x2= 4
34+
1x3= 3 2x3= 6 3x3= 9
35+
1x4= 4 2x4= 8 3x4=12 4x4=16
36+
1x5= 5 2x5=10 3x5=15 4x5=20 5x5=25
37+
1x6= 6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
38+
1x7= 7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
39+
1x8= 8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
40+
1x9= 9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
41+
```
42+
43+
不过,这里我们先要了解如何创建 list 生成式
44+
45+
## 2、list 生成式的创建 ##
46+
47+
首先,lsit 生成式的语法为:
48+
49+
```python
50+
[expr for iter_var in iterable]
51+
[expr for iter_var in iterable if cond_expr]
52+
```
53+
54+
第一种语法:首先迭代 iterable 里所有内容,每一次迭代,都把 iterable 里相应内容放到iter_var 中,再在表达式中应用该 iter_var 的内容,最后用表达式的计算值生成一个列表。
55+
56+
第二种语法:加入了判断语句,只有满足条件的内容才把 iterable 里相应内容放到 iter_var 中,再在表达式中应用该 iter_var 的内容,最后用表达式的计算值生成一个列表。
57+
58+
其实不难理解的,因为是 list 生成式,因此肯定是用 [] 括起来的,然后里面的语句是把要生成的元素放在前面,后面加 for 循环语句或者 for 循环语句和判断语句。
59+
60+
例子:
61+
62+
```python
63+
# -*- coding: UTF-8 -*-
64+
lsit1=[x * x for x in range(1, 11)]
65+
print(lsit1)
66+
```
67+
68+
输出的结果:
69+
70+
```txt
71+
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
72+
```
73+
74+
可以看到,就是把要生成的元素 x * x 放到前面,后面跟 for 循环,就可以把 list 创建出来。那么 for 循环后面有 if 的形式呢?又该如何理解:
75+
76+
```python
77+
# -*- coding: UTF-8 -*-
78+
lsit1= [x * x for x in range(1, 11) if x % 2 == 0]
79+
print(lsit1)
80+
```
81+
82+
输出的结果:
83+
84+
```txt
85+
[4, 16, 36, 64, 100]
86+
```
87+
88+
这个例子是为了求 1 到 10 中偶数的平方根,上面也说到, `x * x` 是要生成的元素,后面那部分其实就是在 for 循环中嵌套了一个 if 判断语句。
89+
90+
那么有了这个知识点,我们也可以猜想出,for 循环里面也嵌套 for 循环。具体示例:
91+
92+
```python
93+
# -*- coding: UTF-8 -*-
94+
lsit1= [(x+1,y+1) for x in range(3) for y in range(5)]
95+
print(lsit1)
96+
```
97+
98+
输出的结果:
99+
100+
```txt
101+
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)]
102+
```
103+
104+
其实知道了 list 生成式是怎样组合的,就不难理解这个东西了。因为 list 生成式只是把之前学习的知识点进行了组合,换成了一种更简洁的写法而已。

0 commit comments

Comments
 (0)