diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-02 21:39:07 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-02 21:39:07 (GMT) |
commit | ad9d96bc1a706302bb94710f8a1bc21dcdddb567 (patch) | |
tree | 653d9c9ccbd310f00047f6aebb4fb47df12dfb12 | |
parent | 80a50acbe9de08730124bf557e042be1829d0838 (diff) | |
download | cpython-ad9d96bc1a706302bb94710f8a1bc21dcdddb567.zip cpython-ad9d96bc1a706302bb94710f8a1bc21dcdddb567.tar.gz cpython-ad9d96bc1a706302bb94710f8a1bc21dcdddb567.tar.bz2 |
Issue #4615. Document how to use itertools for de-duping.
-rw-r--r-- | Doc/library/itertools.rst | 24 | ||||
-rw-r--r-- | Lib/test/test_itertools.py | 36 |
2 files changed, 60 insertions, 0 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index ec0f533..2b27647 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -606,3 +606,27 @@ which incur interpreter overhead. return indices[i:] = [indices[i] + 1] * (r - i) yield tuple(pool[i] for i in indices) + + def unique_everseen(iterable, key=None): + "List unique elements, preserving order. Remember all elements ever seen." + # unique_everseen('AAAABBBCCDAABBB') --> A B C D + # unique_everseen('ABBCcAD', str.lower) --> A B C D + seen = set() + seen_add = seen.add + if key is None: + for element in iterable: + if element not in seen: + seen_add(element) + yield element + else: + for element in iterable: + k = key(element) + if k not in seen: + seen_add(k) + yield element + + def unique_justseen(iterable, key=None): + "List unique elements, preserving order. Remember only the element just seen." + # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B + # unique_justseen('ABBCcAD', str.lower) --> A B C A D + return map(next, map(itemgetter(1), groupby(iterable, key))) diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index a0d45e3..80424f8 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1298,6 +1298,30 @@ Samuele ... indices[i:] = [indices[i] + 1] * (r - i) ... yield tuple(pool[i] for i in indices) +>>> def unique_everseen(iterable, key=None): +... "List unique elements, preserving order. Remember all elements ever seen." +... # unique_everseen('AAAABBBCCDAABBB') --> A B C D +... # unique_everseen('ABBCcAD', str.lower) --> A B C D +... seen = set() +... seen_add = seen.add +... if key is None: +... for element in iterable: +... if element not in seen: +... seen_add(element) +... yield element +... else: +... for element in iterable: +... k = key(element) +... if k not in seen: +... seen_add(k) +... yield element + +>>> def unique_justseen(iterable, key=None): +... "List unique elements, preserving order. Remember only the element just seen." +... # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B +... # unique_justseen('ABBCcAD', str.lower) --> A B C A D +... return map(next, map(itemgetter(1), groupby(iterable, key))) + This is not part of the examples but it tests to make sure the definitions perform as purported. @@ -1360,6 +1384,18 @@ perform as purported. >>> list(combinations_with_replacement('abc', 2)) [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')] +>>> list(unique_everseen('AAAABBBCCDAABBB')) +['A', 'B', 'C', 'D'] + +>>> list(unique_everseen('ABBCcAD', str.lower)) +['A', 'B', 'C', 'D'] + +>>> list(unique_justseen('AAAABBBCCDAABBB')) +['A', 'B', 'C', 'D', 'A', 'B'] + +>>> list(unique_justseen('ABBCcAD', str.lower)) +['A', 'B', 'C', 'A', 'D'] + """ __test__ = {'libreftest' : libreftest} |