mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-25 19:12:51 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = [2, 4, 6]
|
||||
|
||||
for n in x:
|
||||
@@ -14,7 +14,7 @@ for n in x:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
2
|
||||
4
|
||||
6
|
||||
@@ -25,7 +25,7 @@ for n in x:
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = [2, 4, 6]
|
||||
|
||||
for i, n in enumerate(x):
|
||||
@@ -33,7 +33,7 @@ for i, n in enumerate(x):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
pos 0 is 2
|
||||
pos 1 is 4
|
||||
pos 2 is 6
|
||||
@@ -44,14 +44,14 @@ pos 2 is 6
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = [2, 4, 6]
|
||||
i = x.__iter__()
|
||||
print i
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
<listiterator object at 0x0000000003CAE630>
|
||||
|
||||
```
|
||||
@@ -60,12 +60,12 @@ print i
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
print i.next()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
2
|
||||
|
||||
```
|
||||
@@ -74,13 +74,13 @@ print i.next()
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
print i.next()
|
||||
print i.next()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
4
|
||||
6
|
||||
|
||||
@@ -88,12 +88,12 @@ print i.next()
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
i.next()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
StopIteration Traceback (most recent call last)
|
||||
<ipython-input-6-e590fe0d22f8> in <module>()
|
||||
@@ -106,13 +106,13 @@ StopIteration:
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
r = reversed(x)
|
||||
print r
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
<listreverseiterator object at 0x0000000003D615F8>
|
||||
|
||||
```
|
||||
@@ -121,14 +121,14 @@ print r
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
print r.next()
|
||||
print r.next()
|
||||
print r.next()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
6
|
||||
4
|
||||
2
|
||||
@@ -139,14 +139,14 @@ print r.next()
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = {'a':1, 'b':2, 'c':3}
|
||||
i = x.iteritems()
|
||||
print i
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
<dictionary-itemiterator object at 0x0000000003D51B88>
|
||||
|
||||
```
|
||||
@@ -155,24 +155,24 @@ print i
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
print i.__iter__()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
<dictionary-itemiterator object at 0x0000000003D51B88>
|
||||
|
||||
```
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
print i.next()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
('a', 1)
|
||||
|
||||
```
|
||||
@@ -183,7 +183,7 @@ print i.next()
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
class ReverseListIterator(object):
|
||||
|
||||
def __init__(self, list):
|
||||
@@ -204,14 +204,14 @@ class ReverseListIterator(object):
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = range(10)
|
||||
for i in ReverseListIterator(x):
|
||||
print i,
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
9 8 7 6 5 4 3 2 1 0
|
||||
|
||||
```
|
||||
@@ -220,7 +220,7 @@ for i in ReverseListIterator(x):
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
class Collatz(object):
|
||||
|
||||
def __init__(self, start):
|
||||
@@ -249,13 +249,13 @@ class Collatz(object):
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
for x in Collatz(7):
|
||||
print x,
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
|
||||
|
||||
```
|
||||
@@ -264,14 +264,14 @@ for x in Collatz(7):
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
i = Collatz(7)
|
||||
for x, y in zip(i, i):
|
||||
print x, y
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
22 11
|
||||
34 17
|
||||
52 26
|
||||
@@ -287,7 +287,7 @@ for x, y in zip(i, i):
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
class BinaryTree(object):
|
||||
def __init__(self, value, left=None, right=None):
|
||||
self.value = value
|
||||
@@ -301,7 +301,7 @@ class BinaryTree(object):
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
class InorderIterator(object):
|
||||
|
||||
def __init__(self, node):
|
||||
@@ -323,7 +323,7 @@ class InorderIterator(object):
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
tree = BinaryTree(
|
||||
left=BinaryTree(
|
||||
left=BinaryTree(1),
|
||||
@@ -345,13 +345,13 @@ tree = BinaryTree(
|
||||
|
||||
In [20]:
|
||||
|
||||
```
|
||||
```py
|
||||
for value in tree:
|
||||
print value,
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1 2 3 4 5 6 7 8
|
||||
|
||||
```
|
||||
@@ -360,13 +360,13 @@ for value in tree:
|
||||
|
||||
In [21]:
|
||||
|
||||
```
|
||||
```py
|
||||
for x,y in zip(tree, tree):
|
||||
print x, y
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1 1
|
||||
2 2
|
||||
3 3
|
||||
|
||||
Reference in New Issue
Block a user