Created
December 1, 2021 10:16
-
-
Save yszou/52f8899c5fa4903f67102ed3d1fc3ee2 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
# -*- coding: utf-8 -*- | |
todo = [ | |
{"a": 1, "b": 1}, | |
{"a": 2, "b": 2}, | |
{"a": 1, "b": 1}, | |
{"a": 0, "b": 1}, | |
{"a": 2, "b": 2}, | |
{"a": 0, "b": 2}, | |
{"a": 2, "b": 2}, | |
] | |
def gen_key_list(item_list): | |
for o in item_list: | |
yield (o.get('a', None), o.get('b', None)) | |
def find(item_list): | |
idx = -1 | |
output_idx = [] | |
first_idx = {} | |
exists = set() | |
for key in gen_key_list(item_list): | |
idx += 1 | |
if key in exists: | |
if key in first_idx: | |
output_idx.append(first_idx[key]) | |
del first_idx[key] | |
output_idx.append(idx) | |
else: | |
first_idx[key] = idx | |
exists.add(key) | |
return [item_list[idx] for idx in output_idx] | |
if __name__ == '__main__': | |
print(find(todo)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment