python迭代器和解析

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
1、类表解析
    列表解析可以编写任意数量的瞧他的for循环,并且每一个都有可选的if测试,结构如下:
    [expression for target1 in iterable1 [if condition1]
                for target2 in iterable2 [if condition2]
                for target3 in iterable3 [if condition3]
                ...
                for targetN in iteratbleN [if condtionN]
    ]
 
    例如:
    [x+y+z for x in [0,1,2]
           for y in [100,200,300]
           for z in [-1,-2,-3] if z >0
    ]
2、列表解析的速度>map的速度>for的速度
3、如果一个函数含有yield,则这个函数支持迭代器,在for循环中,如果对象支持迭代协议,则使用迭代协议遍历对象,如果对象不支持迭代协议,则通过索引来遍历对象
4、生成器函数的应用
    例如:
    def gensquares(N):
        for i in range(N):
            yield i**2
1、生成器表达式
 
           生成器表达式同列表解析式一样,只是外面的括号换成了圆括号,而不是方括号。这个表达式返回迭代器。
 
                例如:
 
                G = (x**2 for x in range(5))
 
                print(G.__next__())  #输出:0
 
                print(next(G))   #输出:1
 
                print(next(G))  #输出:4
 
2、生成器函数和生成器表达式都是单迭代器对象
 
3、使用迭代工具模拟zipmap
 
                例如:
 
                def mymap(func,*args):
 
                                for arg in zip(*args):
 
                                                yield func(*arg)
 
                或者:
 
                def  mymap(func,*args):
 
                                return (func(*arg) for arg in zip(*args))
 
                                
 
                def myzip(*seqs):
 
                                seqs = [list(s) for s in seqs]
 
                                res = []
 
                                for all(seqs):
 
                                                res.append(tuple(s.pop(0) for s in seqs))
 
                                return res
 
                def myzip(*seqs,pad = None):
 
                                seqs = [list(s) for s in seqs]
 
                                res = []
 
                                for any(seqs):
 
                                                res.append(tuple((s.pop(0) if s else pad) for s in seqs))
 
                                return res
 
4、对迭代的各种方法进行计时
 
                #file mytimer.py   计时文件
 
                import time
 
                reps = 1000
 
                replist = range(reps)
 
                def timer(func , *pargs,**kargs):
 
                                start = time.clock()
 
                                for i in replist:
 
                                                ret = func(*pargs,**kargs)
 
                                elapse = time.clock-start
 
                                return (elapse,ret)
 
                                
 
                                
 
                #file timeseqs.py  测试文件
 
                import sys,mytimer
 
                reps = 1000
 
                repslist = range(reps)
 
                def forLoop():  #for循环
 
                                res = []
 
                                for x in repslist:
 
                                                res.append(abs(x))
 
                                return res
 
                def listcomp():   #列表解析
 
                                return [abs(x) for x in repslist]
 
                def mapCall():  #map函数
 
                                return list(map(abs,repslist))
 
                def genExpr():  #生成器表达式
 
                                return list(abs for x in repslist)
 
                def genFunc():#生成器函数
 
                                def gen():
 
                                                for x in repslist:
 
                                                                yield abs(x)
 
                                return list(gen())
 
                print(sys.version)
 
                for test in (forLoop,listcomp,mapCall,genExpr,genFunc):
 
                                elapsed,result = mytimer.tester(test)
 
                                print(‘-‘*33)
 
                                print(‘%-9s: %.5f  =>[%s...%s] %
 
                                (test.__name__,elapse,result[0],result[-1]))
 
                                
 
                #通过测试可以得出,map速度〉列表解析 〉 生成器函数速度 〉生成器表达式〉for循环

  

python迭代器和解析,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。