Created
May 26, 2015 21:35
-
-
Save rectangletangle/61ffdaad36c60e77fa3b 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
import uuid | |
import random | |
def ordered_uniquification(items): | |
seen = set() | |
for item in items: | |
if not item in seen: | |
seen.add(item) | |
yield item | |
if __name__ == '__main__': | |
total = 100000 | |
original_emails = [str(uuid.uuid1()) + '@example.com' | |
for _ in range(int(total / 2))] # Should be xrange in Python 2.* | |
emails = original_emails * 2 | |
random.shuffle(emails) | |
unique_emails = list(ordered_uniquification(emails)) | |
assert len(unique_emails) == len(original_emails) | |
# A far less efficient one-liner that basically does the same thing | |
# (don't worry, I don't normally write code like this lol) | |
assert unique_emails == [email for _, email in | |
sorted((index, email) | |
for email, index in | |
{email: i | |
for i, email in reversed(list(enumerate(emails)))}.items())] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment