NBA_03_Generatoren¶
(c) 2024 Technische Hochschule Augsburg - Fakultät für Informatik - Prof.Dr.Nik Klever - Impressum
In [1]:
def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]
for char in reverse("golf"):
print(char)
f l o g
In [2]:
gen = reverse("01234")
gen
Out[2]:
<generator object reverse at 0x7ff5584b90f8>
In [3]:
next(gen)
Out[3]:
'4'
In [4]:
next(gen)
Out[4]:
'3'
In [5]:
next(gen)
Out[5]:
'2'
In [6]:
next(gen)
Out[6]:
'1'
In [7]:
next(gen)
Out[7]:
'0'
In [8]:
next(gen)
--------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-8-8a6233884a6c> in <module>() ----> 1 next(gen) StopIteration:
Rückblick: List Comprehension¶
In [9]:
lc = [x for x in range(10) if x%2 == 0]
lc
Out[9]:
[0, 2, 4, 6, 8]
Generator Ausdruck¶
In [10]:
ga = (x for x in range(10) if x%2 == 0)
ga
Out[10]:
<generator object <genexpr> at 0x7ff5584c73b8>
In [11]:
for i in ga:
print(i)
0 2 4 6 8
In [12]:
s = sum(x for x in range(10) if x%2 == 0)
s
Out[12]:
20
In [13]:
from math import pi, sin
sinus_tabelle = {x:sin(x*pi/180) for x in range(0,46)}
sinus_tabelle
Out[13]:
{0: 0.0, 1: 0.01745240643728351, 2: 0.03489949670250097, 3: 0.05233595624294383, 4: 0.0697564737441253, 5: 0.08715574274765817, 6: 0.10452846326765346, 7: 0.12186934340514748, 8: 0.13917310096006544, 9: 0.15643446504023087, 10: 0.17364817766693033, 11: 0.1908089953765448, 12: 0.20791169081775931, 13: 0.224951054343865, 14: 0.24192189559966773, 15: 0.25881904510252074, 16: 0.27563735581699916, 17: 0.29237170472273677, 18: 0.3090169943749474, 19: 0.32556815445715664, 20: 0.3420201433256687, 21: 0.35836794954530027, 22: 0.374606593415912, 23: 0.3907311284892737, 24: 0.40673664307580015, 25: 0.42261826174069944, 26: 0.4383711467890774, 27: 0.45399049973954675, 28: 0.4694715627858908, 29: 0.48480962024633706, 30: 0.49999999999999994, 31: 0.5150380749100542, 32: 0.5299192642332049, 33: 0.5446390350150271, 34: 0.5591929034707469, 35: 0.573576436351046, 36: 0.5877852522924731, 37: 0.6018150231520483, 38: 0.6156614753256582, 39: 0.6293203910498374, 40: 0.6427876096865393, 41: 0.6560590289905072, 42: 0.6691306063588582, 43: 0.6819983600624985, 44: 0.6946583704589973, 45: 0.7071067811865475}
In [14]:
for i in sinus_tabelle:
print(i)
0 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
In [15]:
from pygal import XY
from IPython.display import SVG
sinus_tabelle_als_liste = [(x,sin(x*pi/180)) for x in range(0,361)]
xy = XY()
xy.add("y=sin(x*pi/180)", sinus_tabelle_als_liste)
SVG(xy.render())
Out[15]:
In [19]:
line_liste = [" line 1\n", "line 2 \n", "\n"]
stripped_generator = (line.strip() for line in line_liste)
stripped_generator
Out[19]:
<generator object <genexpr> at 0x7ff54ac7ddb0>
In [20]:
for x in stripped_generator:
print("<{}>".format(x))
<line 1> <line 2> <>
List Comprehension¶
In [21]:
stripped_list = [line.strip() for line in line_liste]
stripped_list
Out[21]:
['line 1', 'line 2', '']
In [22]:
seq1 = "abc"
seq2 = (1,2,3)
z = [(x,y) for x in seq1 for y in seq2]
z
Out[22]:
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]
In [23]:
[x,y for x in seq1 for y in seq2]
File "<ipython-input-23-9fc9191dedc1>", line 1 [x,y for x in seq1 for y in seq2] ^ SyntaxError: invalid syntax
Binärbaum¶
In [24]:
def inorder(t):
if t:
for x in inorder(t.left):
yield x
yield t.label
for x in inorder(t.right):
yield x
In [30]:
class Tree:
def __init__(self, label, left=None, right=None):
self.label = label
self.left = left
self.right = right
def __repr__(self, level=0, indent=" "):
s = level*indent + repr(self.label)
if self.left:
s += "\n" + self.left.__repr__(level+1, indent)
if self.right:
s += "\n" + self.right.__repr__(level+1, indent)
return s
def __iter__(self):
return inorder(self)
In [31]:
def tree(list):
n = len(list)
if n == 0:
return []
i = n // 2
return Tree(list[i], tree(list[:i]), tree(list[i+1:]))
In [32]:
t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
print(t)
'N' 'G' 'D' 'B' 'A' 'C' 'F' 'E' 'K' 'I' 'H' 'J' 'M' 'L' 'U' 'R' 'P' 'O' 'Q' 'T' 'S' 'X' 'W' 'V' 'Z' 'Y'
In [33]:
for x in t:
print(x,end="")
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Übergabe von Parameter an Generatoren¶
In [34]:
def counter(maximum):
i = 0
while i < maximum:
val = (yield i)
if val is not None:
i = val
else:
i += 1
In [35]:
gen = counter(10)
gen
Out[35]:
<generator object counter at 0x7ff55fd34db0>
In [36]:
next(gen)
Out[36]:
0
In [37]:
next(gen)
Out[37]:
1
In [38]:
gen.send(8)
Out[38]:
8
In [39]:
next(gen)
Out[39]:
9
In [40]:
next(gen)
--------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-40-8a6233884a6c> in <module>() ----> 1 next(gen) StopIteration: