Iteratoren

In [1]:
for element in [1,2,3]:
    print(element)
1
2
3
In [2]:
for element in (1,2,3):
    print(element)
1
2
3
In [3]:
for key in {'one':1,'two':2,'three':3}:
    print(key)
three
two
one
In [4]:
for char in "abc":
    print(char)
a
b
c
In [5]:
s = "123"
it = iter(s)
it
Out[5]:
<str_iterator at 0x7fc78c071400>
In [6]:
next(it)
Out[6]:
'1'
In [7]:
next(it)
Out[7]:
'2'
In [8]:
next(it)
Out[8]:
'3'
In [9]:
next(it)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-9-2cdb14c0d4d6> in <module>()
----> 1 next(it)

StopIteration: 
In [10]:
l = [1,2,3]
it = iter(l)
it
Out[10]:
<list_iterator at 0x7fc78c0655f8>
In [11]:
next(it)
Out[11]:
1
In [12]:
next(it)
Out[12]:
2
In [13]:
next(it)
Out[13]:
3
In [14]:
next(it)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-14-2cdb14c0d4d6> in <module>()
----> 1 next(it)

StopIteration: 
In [15]:
class Reverse:
    def __init__(self, data):
        self.data = data
        self.index = len(data)
    
    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index-1
        return self.data[self.index]
    
    def __iter__(self):
        return self
In [16]:
rev = Reverse("spam")
it = iter(rev)
it
Out[16]:
<__main__.Reverse at 0x7fc777774748>
In [17]:
next(it)
Out[17]:
'm'
In [18]:
next(it)
Out[18]:
'a'
In [19]:
next(it)
Out[19]:
'p'
In [20]:
next(it)
Out[20]:
's'
In [21]:
next(it)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-21-2cdb14c0d4d6> in <module>()
----> 1 next(it)

<ipython-input-15-36508ed79b6d> in __next__(self)
      6     def __next__(self):
      7         if self.index == 0:
----> 8             raise StopIteration
      9         self.index = self.index-1
     10         return self.data[self.index]

StopIteration: 
In [22]:
for char in Reverse("spam"):
    print(char)
m
a
p
s
In [23]:
L = [1,2,3]
it = iter(L)
it
Out[23]:
<list_iterator at 0x7fc77777b390>
In [24]:
tuple(it)
Out[24]:
(1, 2, 3)
In [25]:
mon = {'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5,'Jun':6,
       'Jul':7,'Aug':8,'Sep':9,'Oct':10,'Nov':11,'Dec':12}
it = iter(mon)
it
Out[25]:
<dict_keyiterator at 0x7fc77776fc28>
In [26]:
next(it)
Out[26]:
'Mar'
In [27]:
next(it)
Out[27]:
'Aug'
In [28]:
for key in mon:
    print(key,mon[key])
Mar 3
Aug 8
May 5
Nov 11
Jun 6
Feb 2
Oct 10
Apr 4
Dec 12
Jul 7
Sep 9
Jan 1
In [29]:
next(it)
Out[29]:
'May'
In [30]:
for k,v in sorted(mon.items(),key=lambda x: x[1]):
    print(k,v)
Jan 1
Feb 2
Mar 3
Apr 4
May 5
Jun 6
Jul 7
Aug 8
Sep 9
Oct 10
Nov 11
Dec 12
In [32]:
%%writefile myfile.txt
Dies ist wieder
ein mehrzeiliger
Text

mit einigen

Leerzeilen

dazwischen
Writing myfile.txt
In [34]:
f = open("myfile.txt")
for line in f:
    if line.strip() != "":
        print(line,end="")
Dies ist wieder
ein mehrzeiliger
Text
mit einigen
Leerzeilen
dazwischen