只要让 ngrams 函数,用 yield 关键字返回一个生成器,然后内存需求就变成 O(n) 了。
- 1def ngrams(tokens, n):
- 2 length = len(tokens)
- 3 for i in range(length - n + 1):
- 4 yield tokens[i:i+n]
- 5
- 6ngrams_generator = ngrams(tokens, 3)
- 7print(ngrams_generator)
- 8
- 9==> <generator object ngrams at 0x1069b26d0>
- 10
- 11for ngram in ngrams_generator:
- 12 print(ngram)
- 13
- 14==> ['i', 'want', 'to']
- 15 ['want', 'to', 'go']
- 16 ['to', 'go', 'to']
- 17 ['go', 'to', 'school']
还有一种生成n-grams的方法,是用切片来创建列表:[0, 1, …, -n], [1, 2, …, -n+1], …, [n-1, n, …, -1],然后把它们zip到一起。
- 1def ngrams(tokens, n):
- 2 length = len(tokens)
- 3 slices = (tokens[i:length-n+i+1] for i in range(n))
- 4 return zip(*slices)
- 5
- 6ngrams_generator = ngrams(tokens, 3)
- 7print(ngrams_generator)
- 8
- 9==> <zip object at 0x1069a7dc8> # zip objects are generators
- 10
- 11for ngram in ngrams_generator:
- 12 print(ngram)
- 13
- 14==> ('i', 'want', 'to')
- 15 ('want', 'to', 'go')
- 16 ('to', 'go', 'to')
- 17 ('go', 'to', 'school')
注意,创建切片用的是 (tokens[…] for i in range(n)) ,不是 [tokens[…] for i in range(n)] 。
[] 返回的是列表,() 返回的是生成器。
3、类,以及魔术方法
在Python里面,魔术方法 (Magic Methods) 是用双下划线,作为前缀后缀的。
其中,最知名的可能就是 _init_ 了。
- 1class Node:
- 2 """ A struct to denote the node of a binary tree.
- 3 It contains a value and pointers to left and right children.
- 4 """
- 5 def __init__(self, value, left=None, right=None):
- 6 self.value = value
- 7 self.left = left
- 8 self.right = right
不过,如果想输出 (Print) 一个节点 (Node) ,就不是很容易了。
- 1root = Node(5)
- 2print(root) # <__main__.Node object at 0x1069c4518>
理想情况,应该是输出它的值,如果它有子节点的话,也输出子节点的值。
所以,要用魔术方法 _repr_ ,它必须返回一个可输出的object,如字符串。
- 1class Node:
- 2 """ A struct to denote the node of a binary tree.
- 3 It contains a value and pointers to left and right children.
- 4 """
- 5 def __init__(self, value, left=None, right=None):
- 6 self.value = value
- 7 self.left = left
- 8 self.right = right
- 9
- 10 def __repr__(self):
- 11 strings = [f'value: {self.value}']
- 12 strings.append(f'left: {self.left.value}' if self.left else 'left: None')
- 13 strings.append(f'right: {self.right.value}' if self.right else 'right: None')
- 14 return ', '.join(strings)
- 15
- 16left = Node(4)
- 17root = Node(5, left)
- 18print(root) # value: 5, left: 4, right: None
(编辑:ASP站长网)
|