Skip to content

Commit f90849b

Browse files
committed
Issue 1083: Improve docs for unique_in_window()
1 parent 476083a commit f90849b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

more_itertools/more.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4602,7 +4602,7 @@ def is_scalar(obj):
46024602

46034603
def unique_in_window(iterable, n, key=None):
46044604
"""Yield the items from *iterable* that haven't been seen recently.
4605-
*n* is the size of the lookback window.
4605+
*n* is the size of the sliding window.
46064606
46074607
>>> iterable = [0, 1, 0, 2, 3, 0]
46084608
>>> n = 3
@@ -4614,14 +4614,22 @@ def unique_in_window(iterable, n, key=None):
46144614
>>> list(unique_in_window('abAcda', 3, key=lambda x: x.lower()))
46154615
['a', 'b', 'c', 'd', 'a']
46164616
4617+
Updates a sliding window no larger than n and yields a value
4618+
if the item only occurs once in the updated window.
4619+
4620+
When `n == 1`, the *unique_in_window* is memoryless:
4621+
4622+
>>> list(unique_in_window('aab', n=1))
4623+
['a', 'a', 'b']
4624+
46174625
The items in *iterable* must be hashable.
46184626
46194627
"""
46204628
if n <= 0:
46214629
raise ValueError('n must be greater than 0')
46224630

46234631
window = deque(maxlen=n)
4624-
counts = defaultdict(int)
4632+
counts = Counter()
46254633
use_key = key is not None
46264634

46274635
for item in iterable:

0 commit comments

Comments
 (0)