Generatoren

Rückgabewert: wird statt mit return mit yield zurückgegeben

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]:
Pygal-1-1-0.8-0.8-0.6-0.6-0.4-0.4-0.2-0.2000.20.20.40.40.60.60.80.81100404080801201201601602002002402402802803203203603600: 011.115384615384617270.01: 0.0174524064412.659188034188034265.469086790320662: 0.034899496714.202991452991453260.93955374069693: 0.0523359562415.74679487179487256.41278059077424: 0.0697564737417.29059829059829251.890146239505955: 0.0871557427518.83440170940171247.373028325127226: 0.104528463320.378205128205128242.862802805513077: 0.121869343421.92200854700855238.36084353904838: 0.13917310123.46581196581197233.868521866136869: 0.15643446525.009615384615387229.3872061914785310: 0.173648177726.553418803418808224.9182615672392611: 0.190808995428.097222222222225220.4630492772431612: 0.207911690829.641025641025642216.0229264223124213: 0.224951054331.184829059829063211.5992455068811814: 0.241921895632.728632478632484207.1933540270093215: 0.258819045134.2724358974359202.8065940599224716: 0.275637355835.81623931623932198.440301855202117: 0.292371704737.36004273504274194.0958074277510318: 0.309016994438.90384615384616189.7744341526578819: 0.325568154540.447649572649574185.4774983620843620: 0.342020143341.991452991452995181.2063089442975821: 0.358367949543.535256410256416176.9621669449701422: 0.374606593445.07905982905983172.74636517086923: 0.390731128546.62286324786324168.5601877960539824: 0.406736643148.166666666666664164.4049099707057225: 0.422618261749.71047008547009160.28179743270326: 0.438371146851.25427350427351156.1921061220664327: 0.453990499752.798076923076934152.1370817983869328: 0.469471562854.34188034188035148.1179596613552629: 0.484809620255.88568376068377144.1359639745086830: 0.557.42948717948719140.1923076923076831: 0.515038074958.97329059829061136.2881920906589832: 0.529919264260.517094017094024132.424806400994933: 0.54463903562.060897435897445128.6033274480218434: 0.559192903563.604700854700866124.8249192912483635: 0.573576436465.14850427350429121.0907328704015536: 0.587785252366.69230769230771117.4019056548386937: 0.601815023268.23611111111111113.7595612970644138: 0.615661475369.77991452991454110.1648092904541239: 0.62932039171.32371794871796106.6187446312922640: 0.642787609772.86752136752138103.122447485225441: 0.65605902974.411324786324899.6769828582336642: 0.669130606475.9551282051282296.2834002722194743: 0.681998360177.4989316239316492.9427334453128544: 0.694658370579.0427350427350689.6559999769911545: 0.707106781280.5865384615384786.4242010381078746: 0.719339800382.1303418803418983.2483210659270947: 0.731353701683.6741452991453180.1293274642537848: 0.743144825585.2179487179487377.0681703087535149: 0.754709580286.7617521367521574.065782057549650: 0.766044443188.3055555555555671.1230772671884851: 0.777145961589.8493589743589868.2409523140556252: 0.788010753691.393162393162465.420285121331853: 0.7986355192.9369658119658262.66193489156819454: 0.809016994494.4807692307692359.96674184496555455: 0.819152044396.0245726495726557.335526963434856: 0.829037572697.5683760683760754.7690917405180957: 0.838670567999.1121794871794952.2682179372457158: 0.8480480962100.6559829059829149.8336673440048259: 0.8571673007102.1997863247863347.4661815484901760: 0.8660254038103.7435897435897545.1664817098092661: 0.8746197071105.2873931623931642.9352683388107762: 0.8829475929106.8311965811965840.77322108470167463: 0.8910065242108.37538.6809985280199364: 0.8987940463109.9188034188034236.6592379800239365: 0.906307787111.4626068376068434.7085552885620866: 0.9135454576113.0064102564102632.8295446504786867: 0.9205048535114.5502136752136831.0227784306165468: 0.9271838546116.0940170940170929.2888069874686669: 0.9335804265117.6378205128205127.62815850553420270: 0.9396926208119.1816239316239326.0413388344276271: 0.9455185756120.7254273504273524.528831334792772: 0.9510565163122.2692307692307723.0910967310659373: 0.956304756123.813034188034221.7285729711350174: 0.9612616959125.3568376068376220.44167509293640775: 0.9659258263126.9006410256410419.230795098030476: 0.9702957263128.4444444444444618.09630183219326277: 0.9743700648129.9882478632478617.0385408730638878: 0.9781476007131.532051282051316.05783442487745679: 0.9816271834133.075854700854715.15448122031807580: 0.984807753134.619658119658114.32875642952285581: 0.9876883406136.1634615384615513.58091157626233882: 0.9902680687137.7072649572649512.91117446132318583: 0.9925461516139.251068376068412.3197490931183984: 0.9945218954140.794871794871811.80681562554434585: 0.9961946981142.3386752136752311.37253030310455486: 0.9975640503143.8824786324786411.01702541331485587: 0.9986295348145.4262820512820410.74040924640871788: 0.999390827146.9700854700854810.5427660623502189: 0.9998476952148.5138888888888910.42415606516772290: 1150.0576923076923210.38461538461547291: 0.9998476952151.6014957264957310.42415606516772292: 0.999390827153.1452991452991610.5427660623502193: 0.9986295348154.6891025641025710.74040924640871794: 0.9975640503156.2329059829059811.01702541331485595: 0.9961946981157.776709401709411.37253030310455496: 0.9945218954159.3205128205128211.80681562554434597: 0.9925461516160.8643162393162512.3197490931183998: 0.9902680687162.4081196581196612.91117446132318599: 0.9876883406163.951923076923113.580911576262338100: 0.984807753165.495726495726514.328756429522855101: 0.9816271834167.039529914529915.154481220318075102: 0.9781476007168.5833333333333416.057834424877456103: 0.9743700648170.1271367521367517.03854087306388104: 0.9702957263171.6709401709401818.096301832193262105: 0.9659258263173.214743589743619.2307950980304106: 0.9612616959174.7585470085470320.441675092936407107: 0.956304756176.3023504273504621.72857297113501108: 0.9510565163177.8461538461538723.09109673106593109: 0.9455185756179.389957264957324.5288313347927110: 0.9396926208180.933760683760726.04133883442762111: 0.9335804265182.4775641025641227.628158505534202112: 0.9271838546184.0213675213675529.28880698746866113: 0.9205048535185.5651709401709631.022778430616427114: 0.9135454576187.108974358974432.82954465047857115: 0.906307787188.652777777777834.70855528856208116: 0.8987940463190.1965811965812336.659237980024045117: 0.8910065242191.7403846153846438.68099852801993118: 0.8829475929193.2841880341880540.773221084701674119: 0.8746197071194.8279914529914842.93526833881066120: 0.8660254038196.371794871794945.16648170980915121: 0.8571673007197.9155982905982747.466181548490056122: 0.8480480962199.459401709401749.833667344004766123: 0.8386705679201.003205128205152.26821793724571124: 0.8290375726202.5470085470085554.76909174051809125: 0.8191520443204.0908119658119557.3355269634348126: 0.8090169944205.634615384615459.966741844965554127: 0.79863551207.178418803418862.661934891568194128: 0.7880107536208.722222222222265.4202851213318129: 0.7771459615210.2660256410256468.24095231405562130: 0.7660444431211.8098290598290471.12307726718848131: 0.7547095802213.3536324786324874.06578205754971132: 0.7431448255214.8974358974358877.06817030875345133: 0.7313537016216.4412393162393280.12932746425378134: 0.7193398003217.9850427350427383.24832106592697135: 0.7071067812219.5288461538461386.42420103810787136: 0.6946583705221.0726495726495789.65599997699115137: 0.6819983601222.6164529914529792.94273344531285138: 0.6691306064224.160256410256496.28340027221947139: 0.656059029225.7040598290598299.67698285823366140: 0.6427876097227.24786324786325103.12244748522534141: 0.629320391228.79166666666666106.61874463129215142: 0.6156614753230.33547008547006110.16480929045406143: 0.6018150232231.8792735042735113.75956129706447144: 0.5877852523233.4230769230769117.40190565483863145: 0.5735764364234.96688034188034121.09073287040144146: 0.5591929035236.51068376068375124.82491929124836147: 0.544639035238.05448717948718128.60332744802184148: 0.5299192642239.5982905982906132.4248064009949149: 0.5150380749241.142094017094136.28819209065898150: 0.5242.68589743589743140.19230769230768151: 0.4848096202244.22970085470084144.13596397450863152: 0.4694715628245.77350427350427148.11795966135514153: 0.4539904997247.31730769230768152.13708179838693154: 0.4383711468248.86111111111111156.19210612206643155: 0.4226182617250.40491452991452160.281797432703156: 0.4067366431251.94871794871793164.40490997070566157: 0.3907311285253.49252136752136168.56018779605387158: 0.3746065934255.03632478632477172.74636517086896159: 0.3583679495256.5801282051282176.96216694497014160: 0.3420201433258.12393162393164181.20630894429752161: 0.3255681545259.66773504273505185.47749836208425162: 0.3090169944261.21153846153845189.77443415265788163: 0.2923717047262.75534188034186194.09580742775097164: 0.2756373558264.29914529914527198.440301855202165: 0.2588190451265.84294871794873202.80659405992242166: 0.2419218956267.38675213675214207.19335402700932167: 0.2249510543268.93055555555554211.59924550688123168: 0.2079116908270.47435897435895216.02292642231242169: 0.1908089954272.0181623931624220.4630492772431170: 0.1736481777273.5619658119658224.91826156723926171: 0.156434465275.1057692307692229.38720619147853172: 0.139173101276.64957264957263233.8685218661368173: 0.1218693434278.19337606837604238.36084353904823174: 0.1045284633279.7371794871795242.86280280551296175: 0.08715574275281.2809829059829247.3730283251271176: 0.06975647374282.8247863247863251.89014623950584177: 0.05233595624284.3685897435897256.4127805907742178: 0.0348994967285.91239316239313260.93955374069697179: 0.01745240644287.4561965811966265.46908679032066180: 1.224646799e-16289.0269.99999999999994181: -0.01745240644290.5438034188034274.5309132096793182: -0.0348994967292.0876068376068279.0604462593031183: -0.05233595624293.6314102564103283.5872194092257184: -0.06975647374295.1752136752137288.10985376049393185: -0.08715574275296.7190170940171292.6269716748727186: -0.1045284633298.2628205128205297.1371971944869187: -0.1218693434299.8066239316239301.6391564609518188: -0.139173101301.35042735042737306.13147813386314189: -0.156434465302.8942307692308310.6127938085215190: -0.1736481777304.4380341880342315.08173843276086191: -0.1908089954305.9818376068376319.5369507227568192: -0.2079116908307.525641025641323.9770735776874193: -0.2249510543309.06944444444446328.4007544931188194: -0.2419218956310.61324786324786332.8066459729906195: -0.2588190451312.15705128205127337.19340594007735196: -0.2756373558313.7008547008547341.55969814479784197: -0.2923717047315.24465811965814345.9041925722488198: -0.3090169944316.78846153846155350.2255658473422199: -0.3255681545318.33226495726495354.52250163791564200: -0.3420201433319.87606837606836358.7936910557024201: -0.3583679495321.41987179487177363.03783305503202: -0.3746065934322.96367521367523367.253634829131203: -0.3907311285324.50747863247864371.4398122039461204: -0.4067366431326.05128205128204375.59509002929417205: -0.4226182617327.59508547008545379.7182025672969206: -0.4383711468329.13888888888886383.80789387793345207: -0.4539904997330.6826923076923387.86291820161296208: -0.4694715628332.2264957264957391.88204033864474209: -0.4848096202333.77029914529913395.8640360254914210: -0.5335.31410256410254399.8076923076923211: -0.5150380749336.857905982906403.71180790934096212: -0.5299192642338.4017094017094407.57519359900505213: -0.544639035339.9455128205128411.3966725519782214: -0.5591929035341.4893162393162415.1750807087515215: -0.5735764364343.03311965811963418.90926712959845216: -0.5877852523344.5769230769231422.59809434516126217: -0.6018150232346.1207264957265426.24043870293553218: -0.6156614753347.6645299145299429.83519070954577219: -0.629320391349.2083333333333433.38125536870785220: -0.6427876097350.7521367521368436.8775525147746221: -0.656059029352.2959401709402440.32301714176634222: -0.6691306064353.83974358974365443.7165997277805223: -0.6819983601355.38354700854705447.0572665546871224: -0.6946583705356.92735042735046450.3440000230089225: -0.7071067812358.47115384615387453.57579896189213226: -0.7193398003360.0149572649573456.7516789340728227: -0.7313537016361.55876068376074459.87067253574605228: -0.7431448255363.10256410256414462.93182969124655229: -0.7547095802364.64636752136755465.93421794245035230: -0.7660444431366.19017094017096468.8769227328116231: -0.7771459615367.7339743589744471.75904768594444232: -0.7880107536369.2777777777778474.57971487866826233: -0.79863551370.82158119658123477.3380651084318234: -0.8090169944372.36538461538464480.0332581550344235: -0.8191520443373.90918803418805482.66447303656514236: -0.8290375726375.4529914529915485.2309082594819237: -0.8386705679376.9967948717949487.7317820627543238: -0.8480480962378.5405982905983490.1663326559952239: -0.8571673007380.08440170940173492.5338184515099240: -0.8660254038381.6282051282052494.83351829019074241: -0.8746197071383.1720085470086497.06473166118934242: -0.8829475929384.715811965812499.2267789152983243: -0.8910065242386.2596153846154501.3190014719801244: -0.8987940463387.8034188034188503.340762019976245: -0.906307787389.3472222222223505.2914447114379246: -0.9135454576390.8910256410257507.1704553495214247: -0.9205048535392.4348290598291508.9772215693835248: -0.9271838546393.9786324786325510.71119301253134249: -0.9335804265395.5224358974359512.3718414944658250: -0.9396926208397.0662393162394513.9586611655723251: -0.9455185756398.6100427350428515.4711686652073252: -0.9510565163400.1538461538462516.9089032689341253: -0.956304756401.6976495726496518.271427028865254: -0.9612616959403.24145299145306519.5583249070636255: -0.9659258263404.78525641025647520.7692049019696256: -0.9702957263406.3290598290599521.9036981678067257: -0.9743700648407.8728632478633522.961459126936258: -0.9781476007409.4166666666667523.9421655751225259: -0.9816271834410.96047008547015524.8455187796819260: -0.984807753412.50427350427356525.671243570477261: -0.9876883406414.04807692307696526.4190884237377262: -0.9902680687415.59188034188037527.0888255386769263: -0.9925461516417.1356837606838527.6802509068817264: -0.9945218954418.67948717948724528.1931843744555265: -0.9961946981420.22329059829065528.6274696968954266: -0.9975640503421.76709401709405528.9829745866851267: -0.9986295348423.31089743589746529.2595907535913268: -0.999390827424.8547008547009529.4572339376498269: -0.9998476952426.39850427350433529.5758439348324270: -1427.94230769230774529.6153846153846271: -0.9998476952429.48611111111114529.5758439348324272: -0.999390827431.02991452991455529.4572339376499273: -0.9986295348432.573717948718529.2595907535913274: -0.9975640503434.1175213675214528.9829745866851275: -0.9961946981435.6613247863248528.6274696968954276: -0.9945218954437.20512820512823528.1931843744555277: -0.9925461516438.74893162393164527.6802509068816278: -0.9902680687440.2927350427351527.0888255386769279: -0.9876883406441.8365384615385526.4190884237377280: -0.984807753443.3803418803419525.6712435704771281: -0.9816271834444.9241452991453524.845518779682282: -0.9781476007446.4679487179488523.9421655751227283: -0.9743700648448.0117521367522522.961459126936284: -0.9702957263449.5555555555556521.9036981678069285: -0.9659258263451.099358974359520.7692049019696286: -0.9612616959452.6431623931624519.5583249070635287: -0.956304756454.1869658119659518.271427028865288: -0.9510565163455.7307692307693516.9089032689341289: -0.9455185756457.2745726495727515.4711686652073290: -0.9396926208458.8183760683761513.9586611655724291: -0.9335804265460.3621794871795512.3718414944659292: -0.9271838546461.90598290598297510.71119301253134293: -0.9205048535463.4497863247864508.9772215693836294: -0.9135454576464.9935897435898507.1704553495214295: -0.906307787466.5373931623932505.291444711438296: -0.8987940463468.08119658119665503.34076201997607297: -0.8910065242469.62500000000006501.31900147198013298: -0.8829475929471.16880341880346499.2267789152984299: -0.8746197071472.71260683760687497.06473166118934300: -0.8660254038474.2564102564103494.8335182901908301: -0.8571673007475.80021367521374492.53381845150994302: -0.8480480962477.34401709401715490.16633265599523303: -0.8386705679478.88782051282055487.73178206275435304: -0.8290375726480.43162393162396485.2309082594821305: -0.8191520443481.97542735042737482.66447303656514306: -0.8090169944483.51923076923083480.03325815503445307: -0.79863551485.06303418803424477.33806510843186308: -0.7880107536486.60683760683764474.57971487866814309: -0.7771459615488.15064102564105471.7590476859443310: -0.7660444431489.6944444444445468.87692273281164311: -0.7547095802491.2382478632479465.93421794245046312: -0.7431448255492.7820512820513462.93182969124666313: -0.7313537016494.32585470085473459.87067253574634314: -0.7193398003495.86965811965814456.751678934073315: -0.7071067812497.4134615384616453.5757989618922316: -0.6946583705498.957264957265450.34400002300896317: -0.6819983601500.5010683760684447.05726655468703318: -0.6691306064502.0448717948718443.7165997277805319: -0.656059029503.5886752136753440.32301714176634320: -0.6427876097505.1324786324787436.8775525147747321: -0.629320391506.6762820512821433.3812553687079322: -0.6156614753508.2200854700855429.83519070954605323: -0.6018150232509.7638888888889426.2404387029356324: -0.5877852523511.3076923076924422.59809434516137325: -0.5735764364512.8514957264957418.9092671295986326: -0.5591929035514.3952991452992415.1750807087517327: -0.544639035515.9391025641027411.39667255197816328: -0.5299192642517.482905982906407.5751935990054329: -0.5150380749519.0267094017095403.711807909341330: -0.5520.5705128205128399.8076923076924331: -0.4848096202522.1143162393163395.86403602549126332: -0.4694715628523.6581196581197391.88204033864474333: -0.4539904997525.2019230769231387.8629182016132334: -0.4383711468526.7457264957266383.80789387793345335: -0.4226182617528.2895299145299379.7182025672971336: -0.4067366431529.8333333333334375.5950900292943337: -0.3907311285531.3771367521368371.4398122039463338: -0.3746065934532.9209401709402367.2536348291311339: -0.3583679495534.4647435897436363.03783305503340: -0.3420201433536.0085470085471358.7936910557024341: -0.3255681545537.5523504273505354.5225016379159342: -0.3090169944539.0961538461539350.2255658473422343: -0.2923717047540.6399572649573345.9041925722488344: -0.2756373558542.1837606837607341.559698144798345: -0.2588190451543.7275641025642337.19340594007747346: -0.2419218956545.2713675213676332.8066459729907347: -0.2249510543546.815170940171328.4007544931189348: -0.2079116908548.3589743589744323.97707357768763349: -0.1908089954549.9027777777778319.5369507227567350: -0.1736481777551.4465811965813315.081738432761351: -0.156434465552.9903846153846310.61279380852153352: -0.139173101554.5341880341881306.13147813386325353: -0.1218693434556.0779914529915301.6391564609519354: -0.1045284633557.6217948717949297.13719719448693355: -0.08715574275559.1655982905984292.62697167487283356: -0.06975647374560.7094017094017288.10985376049393357: -0.05233595624562.2532051282052283.58721940922595358: -0.0348994967563.7970085470085279.0604462593031359: -0.01745240644565.340811965812274.5309132096797360: -2.449293598e-16566.8846153846155270.00000000000006y=sin(x*pi/180)

Zusammenfassung

Generator Ausdruck

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: