Last active
June 19, 2022 11:43
-
-
Save e96031413/7189f6acd374219754e998829b837d35 to your computer and use it in GitHub Desktop.
This file contains 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
# 無重複數字 | |
# 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'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference: https://stackoverflow.com/a/6294744/13369757