mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-01 14:00:18 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
`while` 循环通常有这样的形式:
|
||||
|
||||
```
|
||||
```py
|
||||
<do setup>
|
||||
result = []
|
||||
while True:
|
||||
@@ -15,7 +15,7 @@ while True:
|
||||
|
||||
使用迭代器实现这样的循环:
|
||||
|
||||
```
|
||||
```py
|
||||
class GenericIterator(object):
|
||||
def __init__(self, ...):
|
||||
<do setup>
|
||||
@@ -33,7 +33,7 @@ class GenericIterator(object):
|
||||
|
||||
更简单的,可以使用生成器:
|
||||
|
||||
```
|
||||
```py
|
||||
def generator(...):
|
||||
<do setup>
|
||||
while True:
|
||||
@@ -51,7 +51,7 @@ def generator(...):
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
def collatz(n):
|
||||
sequence = []
|
||||
while n != 1:
|
||||
@@ -67,7 +67,7 @@ for x in collatz(7):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
|
||||
|
||||
```
|
||||
@@ -76,7 +76,7 @@ for x in collatz(7):
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
class Collatz(object):
|
||||
def __init__(self, start):
|
||||
self.value = start
|
||||
@@ -98,7 +98,7 @@ for x in Collatz(7):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
|
||||
|
||||
```
|
||||
@@ -107,7 +107,7 @@ for x in Collatz(7):
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
def collatz(n):
|
||||
while n != 1:
|
||||
if n % 2 == 0:
|
||||
@@ -121,7 +121,7 @@ for x in collatz(7):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
|
||||
|
||||
```
|
||||
@@ -130,13 +130,13 @@ for x in collatz(7):
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = collatz(7)
|
||||
print x
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
<generator object collatz at 0x0000000003B63750>
|
||||
|
||||
```
|
||||
@@ -145,13 +145,13 @@ print x
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
print x.next()
|
||||
print x.next()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
22
|
||||
11
|
||||
|
||||
@@ -161,12 +161,12 @@ print x.next()
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
print x.__iter__()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
<generator object collatz at 0x0000000003B63750>
|
||||
|
||||
```
|
||||
@@ -175,7 +175,7 @@ print x.__iter__()
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
class BinaryTree(object):
|
||||
def __init__(self, value, left=None, right=None):
|
||||
self.value = value
|
||||
@@ -206,7 +206,7 @@ class BinaryTree(object):
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
def inorder(self):
|
||||
node = self
|
||||
stack = []
|
||||
@@ -222,7 +222,7 @@ def inorder(self):
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
tree = BinaryTree(
|
||||
left=BinaryTree(
|
||||
left=BinaryTree(1),
|
||||
@@ -244,7 +244,7 @@ for value in tree:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1 2 3 4 5 6 7 8
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user