[Python-checkins] r68181 - in python/branches/py3k: Doc/library/itertools.rst Lib/test/test_itertools.py
raymond.hettinger
python-checkins at python.org
Fri Jan 2 22:39:08 CET 2009
Author: raymond.hettinger
Date: Fri Jan 2 22:39:07 2009
New Revision: 68181
Log:
Issue #4615. Document how to use itertools for de-duping.
Modified:
python/branches/py3k/Doc/library/itertools.rst
python/branches/py3k/Lib/test/test_itertools.py
Modified: python/branches/py3k/Doc/library/itertools.rst
==============================================================================
--- python/branches/py3k/Doc/library/itertools.rst (original)
+++ python/branches/py3k/Doc/library/itertools.rst Fri Jan 2 22:39:07 2009
@@ -606,3 +606,27 @@
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)))
Modified: python/branches/py3k/Lib/test/test_itertools.py
==============================================================================
--- python/branches/py3k/Lib/test/test_itertools.py (original)
+++ python/branches/py3k/Lib/test/test_itertools.py Fri Jan 2 22:39:07 2009
@@ -1298,6 +1298,30 @@
... 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 @@
>>> 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}
More information about the Python-checkins
mailing list