Solution 1:
l = [1, 3, 7, 3]
unique_keys = set(l)
mappings = {key: val for val, key in enumerate(sorted(unique_keys), 1)}
print(list(map(mappings.get, l)))
Orders are preserved by the sorted()
.
l = [1, 3, 7, 3]
unique_keys = set(l)
mappings = {key: val for val, key in enumerate(sorted(unique_keys), 1)}
print(list(map(mappings.get, l)))
Orders are preserved by the sorted()
.