You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,8 +115,8 @@ print(new_elems)
115
115
print(d)
116
116
117
117
==>1
118
-
[2, 3]
119
-
4
118
+
[2, 3]
119
+
4
120
120
```
121
121
122
122
### 2.2 Slicing
@@ -130,7 +130,7 @@ print(elems)
130
130
131
131
print(elems[::-1])
132
132
133
-
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
133
+
==>[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
134
134
```
135
135
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]`.
We can also use slicing to delete all the even numbers in the list.
@@ -224,9 +224,9 @@ def ngrams(tokens, n):
224
224
print(ngrams(tokens, 3))
225
225
226
226
==> [['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']]
230
230
```
231
231
232
232
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:
248
248
print(ngram)
249
249
250
250
==> ['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']
254
254
```
255
255
256
256
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:
270
270
print(ngram)
271
271
272
272
==> ('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')
276
276
```
277
277
278
278
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