Created
October 22, 2014 11:21
-
-
Save Ogreman/5da3cecf458216157780 to your computer and use it in GitHub Desktop.
deduper with history of duplicates and support for hashable types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
def dedupe_with_history(items, key=None, history=None): | |
previous = deque(maxlen=history) | |
seen = set() | |
for item in items: | |
val = item if key is None else key(item) | |
if val not in seen: | |
yield item | |
seen.add(val) | |
else: | |
previous.append(item) | |
yield previous | |
""" | |
>> list(dedupe_with_history([5, 1, 1, 3, 66, 8, 66]) | |
[5, 1, 3, 66, 8, deque([1, 66])] | |
>> *items, list(dupes) = dedupe_with_history([5, 1, 1, 3, 66, 8, 66]) | |
>> items | |
[5, 1, 3, 66, 8] | |
>> dupes | |
[1, 66] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment