Skip to content

Commit a2da978

Browse files
author
chipn@nvidia.com
committed
spaces to tabs
1 parent 48bb598 commit a2da978

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ print(new_elems)
115115
print(d)
116116

117117
==> 1
118-
[2, 3]
119-
4
118+
[2, 3]
119+
4
120120
```
121121

122122
### 2.2 Slicing
@@ -130,7 +130,7 @@ print(elems)
130130

131131
print(elems[::-1])
132132

133-
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
133+
==> [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
134134
```
135135
The syntax `[x:y:z]` means "take every zth element of a list from index x to index y". When z is negative, it means going backwards. When x isn't specified, it's default to the first element of the list in the direction you traverse the list. When y isn't specified, it's default to the last element of the list. So if we want to take every 2th element of a list, we use `[::2]`.
136136

@@ -142,7 +142,7 @@ reversed_evens = elems[-2::-2]
142142
print(reversed_evens)
143143

144144
==> [0, 2, 4, 6, 8]
145-
[8, 6, 4, 2, 0]
145+
[8, 6, 4, 2, 0]
146146
```
147147

148148
We can also use slicing to delete all the even numbers in the list.
@@ -224,9 +224,9 @@ def ngrams(tokens, n):
224224
print(ngrams(tokens, 3))
225225

226226
==> [['i', 'want', 'to'],
227-
['want', 'to', 'go'],
228-
['to', 'go', 'to'],
229-
['go', 'to', 'school']]
227+
['want', 'to', 'go'],
228+
['to', 'go', 'to'],
229+
['go', 'to', 'school']]
230230
```
231231

232232
In the above example, we have to store all the n-grams at the same time. If the text has m tokens, then the memory requirement is `O(nm)`, which can be problematic when m is large.
@@ -248,9 +248,9 @@ for ngram in ngrams_generator:
248248
print(ngram)
249249

250250
==> ['i', 'want', 'to']
251-
['want', 'to', 'go']
252-
['to', 'go', 'to']
253-
['go', 'to', 'school']
251+
['want', 'to', 'go']
252+
['to', 'go', 'to']
253+
['go', 'to', 'school']
254254
```
255255

256256
Another way is generate n-grams is to use slices to create lists: `[0, 1, ..., -n]`, `[1, 2, ..., -n+1]`, ..., `[n-1, n, ..., -1]`, and then `zip` them together.
@@ -270,9 +270,9 @@ for ngram in ngrams_generator:
270270
print(ngram)
271271

272272
==> ('i', 'want', 'to')
273-
('want', 'to', 'go')
274-
('to', 'go', 'to')
275-
('go', 'to', 'school')
273+
('want', 'to', 'go')
274+
('to', 'go', 'to')
275+
('go', 'to', 'school')
276276
```
277277

278278
Note that to create slices, we use `(tokens[...] for i in range(n))` instead of `[tokens[...] for i in range(n)]`. `[]` is the normal list comprehension that returns a list. `()` returns a generator.

0 commit comments

Comments
 (0)