In [2]:
import re
import reprlib
RE_WORD = re.compile('\w+')
class Sentence:
def __init__(self, text):
self.text = text
self.words = RE_WORD.findall(text)
def __getitem__(self, index):
return self.words[index]
def __len__(self):
return len(self.words)
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text)
In [3]:
s = Sentence('"The time has come, " the Walrus said,')
s
Out[3]:
In [4]:
for word in s:
print(word)
In [5]:
list(s)
Out[5]:
In [6]:
s[0]
Out[6]:
In [7]:
s[5]
Out[7]:
In [8]:
s[-1]
Out[8]:
In [9]:
class Foo:
def __iter__(self):
pass
In [10]:
from collections import abc
In [11]:
issubclass(Foo, abc.Iterable)
Out[11]:
In [12]:
f = Foo()
isinstance(f, abc.Iterable)
Out[12]:
In [13]:
issubclass(Sentence, abc.Iterable)
Out[13]:
In [14]:
s = 'ABC'
for char in s:
print(char)
In [15]:
s = 'ABC'
it = iter(s)
while True:
try:
print(next(it))
except StopIteration:
del it
break
In [16]:
s3 = Sentence('Pig and Pepper')
it = iter(s3)
it
Out[16]:
In [17]:
next(it)
Out[17]:
In [18]:
next(it)
Out[18]:
In [19]:
next(it)
Out[19]:
In [20]:
next(it)
In [21]:
list(it)
Out[21]:
In [22]:
list(iter(s3))
Out[22]:
In [23]:
import re
import reprlib
RE_WORD = re.compile('\w+')
class SentenceIterator:
def __init__(self, words):
self.words = words
self.index = 0
def __next__(self):
try:
word = self.words[self.index]
except IndexError:
raise StopIteration()
self.index += 1
return word
def __iter__(self):
return self
class Sentence:
def __init__(self, text):
self.text = text
self.words = RE_WORD.findall(text)
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text)
def __iter__(self):
return SentenceIterator(self.words)
In [24]:
s = Sentence('"The time has come, " the Walrus said,')
s
Out[24]:
In [25]:
for word in s:
print(word)
In [26]:
list(s)
Out[26]:
In [27]:
import re
import reprlib
RE_WORD = re.compile('\w+')
class Sentence:
def __init__(self, text):
self.text = text
self.words = RE_WORD.findall(text)
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text)
def __iter__(self):
for word in self.words:
yield word
return
In [28]:
def gen_123():
yield 1
yield 2
yield 3
In [29]:
gen_123
Out[29]:
In [30]:
gen_123()
Out[30]:
In [33]:
for i in gen_123():
print(i)
In [34]:
g = gen_123()
next(g)
Out[34]:
In [35]:
next(g)
Out[35]:
In [36]:
next(g)
Out[36]:
In [37]:
next(g)
In [38]:
def gen_AB():
print('start')
yield 'A'
print('continue')
yield 'B'
print('end.')
In [39]:
for c in gen_AB():
print('-->', c)
In [40]:
import re
import reprlib
RE_WORD = re.compile('\w+')
class Sentence:
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text)
def __iter__(self):
for match in RE_WORD.finditer(self.text):
yield match.group()
In [41]:
res1 = [x*3 for x in gen_AB()]
In [42]:
for i in res1:
print('-->', i)
In [45]:
res2 = (x*3 for x in gen_AB())
In [46]:
res2
Out[46]:
In [47]:
for i in res2:
print('-->', i)
In [48]:
import re
import reprlib
RE_WORD = re.compile('\w+')
class Sentence:
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text)
def __iter__(self):
return (match.group() for match in RE_WORD.finditer(self.text))
In [49]:
class ArithmeticProgression:
def __init__(self, begin, step, end=None):
self.begin = begin
self.step = step
self.end = end
def __iter__(self):
result = type(self.begin + self.step)(self.begin)
forever = self.end is None
index = 0
while forever or result < self.end:
yield result
index += 1
result = self.begin + self.step * index
In [50]:
ap = ArithmeticProgression(0, 1, 3)
In [51]:
list(ap)
Out[51]:
In [52]:
ap = ArithmeticProgression(1, .5, 3)
In [53]:
list(ap)
Out[53]:
In [54]:
ap = ArithmeticProgression(0, 1/3, 1)
list(ap)
Out[54]:
In [55]:
from fractions import Fraction
ap = ArithmeticProgression(0, Fraction(1, 3), 1)
list(ap)
Out[55]:
In [56]:
from decimal import Decimal
ap = ArithmeticProgression(0, Decimal('.1'), .3)
list(ap)
Out[56]:
In [61]:
def aritprog_gen(begin, step, end=None):
result = type(begin + step)(begin)
forever = end is None
index = 0
while forever or result < end:
yield result
index += 1
result = begin + step * index
In [62]:
from decimal import Decimal
ap = aritprog_gen(0, Decimal('.1'), .3)
list(ap)
Out[62]:
In [63]:
import itertools
gen = itertools.count(1, .5)
next(gen)
Out[63]:
In [64]:
next(gen)
Out[64]:
In [65]:
next(gen)
Out[65]:
In [66]:
next(gen)
Out[66]:
In [67]:
gen = itertools.takewhile(lambda n: n < 3, itertools.count(1, .5))
list(gen)
Out[67]:
In [68]:
import itertools
In [69]:
def aritprog_gen(begin, step, end=None):
first = type(begin + step)(begin)
ap_gen = itertools.count(first, step)
if end is not None:
ap_gen = itertools.takewhile(lambda: n < end, ap_gen)
return ap_gen
In [70]:
def vowel(c):
return c.lower() in 'aeiou'
In [71]:
list(filter(vowel, 'Aardvark'))
Out[71]:
In [72]:
import itertools
In [73]:
list(itertools.filterfalse(vowel, 'Aardvark'))
Out[73]:
In [74]:
list(itertools.dropwhile(vowel, 'Aardvark'))
Out[74]:
In [75]:
list(itertools.takewhile(vowel, 'Aardvark'))
Out[75]:
In [76]:
list(itertools.compress('Aardvark', (1,0,1,1,0,1)))
Out[76]:
In [78]:
list(itertools.islice('Aardvark', 4))
Out[78]:
In [79]:
list(itertools.islice('Aardvark', 4, 7))
Out[79]:
In [80]:
list(itertools.islice('Aardvark', 1, 7, 2))
Out[80]:
In [81]:
sample = [5, 4, 2, 8, 7, 6, 3, 0, 9, 1]
import itertools
list(itertools.accumulate(sample))
Out[81]:
In [82]:
list(itertools.accumulate(sample, min))
Out[82]:
In [83]:
list(itertools.accumulate(sample, max))
Out[83]:
In [84]:
import operator
list(itertools.accumulate(sample, operator.mul))
Out[84]:
In [85]:
list(itertools.accumulate(range(1, 11), operator.mul))
Out[85]:
In [87]:
print(list(enumerate('albatroz', 1)))
In [88]:
import operator
list(map(operator.mul, range(11), range(11)))
Out[88]:
In [89]:
list(map(operator.mul, range(11), [2, 4, 8]))
Out[89]:
In [90]:
list(map(lambda a, b: (a, b), range(11), [2, 4, 8]))
Out[90]:
In [91]:
import itertools
list(itertools.starmap(operator.mul, enumerate('albatroz', 1)))
Out[91]:
In [95]:
sample = [5, 4, 2, 8, 7, 6, 3, 0, 9, 1]
print(list(itertools.starmap(lambda a, b: b/a,enumerate(itertools.accumulate(sample), 1))))
In [96]:
list(itertools.chain('ABC', range(2)))
Out[96]:
In [97]:
list(itertools.chain(enumerate('ABC')))
Out[97]:
In [98]:
list(itertools.chain.from_iterable(enumerate('ABC')))
Out[98]:
In [99]:
list(zip('ABC', range(5)))
Out[99]:
In [100]:
list(zip('ABC', range(5), [10, 20, 30, 40]))
Out[100]:
In [102]:
list(itertools.zip_longest('ABC', range(5)))
Out[102]:
In [103]:
list(itertools.zip_longest('ABC', range(5), fillvalue='?'))
Out[103]:
In [ ]: