Created
February 18, 2019 07:14
-
-
Save jreisinger/189982e5fadc09b1193c555e6b850ddc to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
from pprint import pprint as pp | |
def dedup2(items, key=None): | |
""" Get unique items but keep the order. | |
Works even with non-hashable items. | |
""" | |
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) | |
logs =\ | |
[ | |
{ "ip": "1.2.3.4", "server": "reisinge.net", "id0": "2", "uri": "/", "id1": "3" }, | |
{ "ip": "1.2.3.5", "server": "reisinge.net", "id0": "2", "uri": "/", "id1": "3" }, | |
{ "ip": "1.2.3.5", "server": "reisinge.net", "id0": "2", "uri": "/", "id1": "3" }, | |
] | |
logs2 = list(dedup2( logs, key=lambda d: (d['id0'], d['ip']) )) | |
pp(logs2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment