Skip to content

Instantly share code, notes, and snippets.

@yszou
Created December 1, 2021 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yszou/52f8899c5fa4903f67102ed3d1fc3ee2 to your computer and use it in GitHub Desktop.
Save yszou/52f8899c5fa4903f67102ed3d1fc3ee2 to your computer and use it in GitHub Desktop.
# -*- 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