-
Notifications
You must be signed in to change notification settings - Fork 0
/
generators.py
88 lines (69 loc) · 1.49 KB
/
generators.py
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
'''
List-version Generator-version
enumerate
reversed
zip itertools.izip
map itertools.imap
sorted N/A
median N/A
f.readlines() for line in f
# All generators are iterators generators are iterators that take minimize space
'''
# def count_to_three():
#
# # print 'a'
# yield 1
# # print 'b'
# yield 2
# # print 'c'
# yield 3
#
# g = count_to_three()
import random
def shuffled(seq):
indices = range(len(seq))
random.shuffle(indices)
for i in indices:
# print seq[i]
yield seq[i]
def myipam(function, iterable):
'''
based on the mymap function
def mymap(function, iterable):
result = []
for element in iterable:
result.append(function(element))
return result
:param function:
:param iterable:
:return:
'''
for element in iterable:
yield function(element)
def myipam(function, iterable):
'''
alternative way
'''
return (function(x) for x in iterable)
# in python 3
# yield from (function(x) for x in iterable)
'''
List comprehensions can be transformed in generators
List form:
In[3]: [s.upper() for s in 'hello']
Out[3]: ['H', 'E', 'L', 'L', 'O']
Generator form:
In[4]: (s.upper() for s in 'hello')
Out[4]: <generator object <genexpr> at 0x1051b9d20>
In[5]: for i in (s.upper() for s in 'hello'): print i
H
E
L
L
O
'''
def myenumerate(seq, start=0):
current = start
for element in seq:
yield current, element
current =+ 1