Skip to content

Instantly share code, notes, and snippets.

@e96031413
Last active June 19, 2022 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e96031413/7189f6acd374219754e998829b837d35 to your computer and use it in GitHub Desktop.
Save e96031413/7189f6acd374219754e998829b837d35 to your computer and use it in GitHub Desktop.
# 無重複數字
# input: name_list = [0, 1, 2, 3, 8]
for idx, num in enumerate(set(name_list)):
if idx != num:
name_list[idx] = idx
# output: name_list = [0, 1, 2, 3, 4]
# 有重複數字
# input: name_list = [0, 2, 2, 1, 6, 6]
for idx, num in enumerate(set(name_list)):
if idx != num:
item = np.where(np.asarray(name_list) == num)[0]
for i in item:
name_list[i] = idx
# output: name_list = [0, 2, 2, 1, 3, 3]
# input: idx_to_class = {0: 'good', 2: 'bad'}
for idx, key in enumerate(sorted(list(id_to_class))):
if idx != key:
idx_to_class[idx] = idx_to_class.pop(key)
# output: idx_to_class = {0: 'good', 1: 'bad'}
@e96031413
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment