summaryrefslogtreecommitdiff
path: root/list_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'list_utils.py')
-rw-r--r--list_utils.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/list_utils.py b/list_utils.py
index 182e2bc..a8030e3 100644
--- a/list_utils.py
+++ b/list_utils.py
@@ -100,12 +100,35 @@ def dedup_list(lst: List[Any]) -> List[Any]:
def uniq(lst: List[Any]) -> List[Any]:
"""
Alias for dedup_list.
-
"""
return dedup_list(lst)
def ngrams(lst: Sequence[Any], n):
+ """
+ Return the ngrams in the sequence.
+
+ >>> seq = 'encyclopedia'
+ >>> for _ in ngrams(seq, 3):
+ ... _
+ 'enc'
+ 'ncy'
+ 'cyc'
+ 'ycl'
+ 'clo'
+ 'lop'
+ 'ope'
+ 'ped'
+ 'edi'
+ 'dia'
+
+ >>> seq = ['this', 'is', 'an', 'awesome', 'test']
+ >>> for _ in ngrams(seq, 3):
+ ... _
+ ['this', 'is', 'an']
+ ['is', 'an', 'awesome']
+ ['an', 'awesome', 'test']
+ """
for i in range(len(lst) - n + 1):
yield lst[i:i + n]