我们可以给列表中的所有数字分配一个唯一的值,重复时它会保留给它的值。这是一个非常常见的问题,在Web开发中,处理物品id时会遇到。让我们讨论一下解决这个问题的一些方法。
1. 使用enumerate() + 列表解析
# initializing list
test_list = [1, 4, 6, 1, 4, 5, 6]# printing the original list
print("The original list is : " + str(test_list))# using list comprehension + enumerate
# assign unique value to list elements
temp = {i: j for j, i in enumerate(set(test_list))}
res = [temp[i] for i in test_list]# printing result
print("The unique value list is : " + str(res))
输出
The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 1, 3, 0, 1, 2, 3]
2. 使用setdefault() + map() + count()
from itertools import count# initializing list
test_list = [1, 4, 6, 1, 4, 5, 6]# printing the original list
print("The original list is : " + str(test_list))# using setdefault() + map() + count()
# assign unique value to list elements
res = list(map({}.setdefault, test_list, count()))# printing result
print("The unique value list is : " + str(res))
输出
The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 1, 2, 0, 1, 5, 2]
3. 使用in,not in操作符和index
# initializing list
test_list = [1, 4, 6, 1, 4, 5, 6]# printing the original list
print ("The original list is : " + str(test_list))# assign unique value to list elements
x=[]
for i in test_list:if i not in x:x.append(i)
res=[]
for i in test_list:res.append(x.index(i))# printing result
print ("The unique value list is : " + str(res))
输出
The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 1, 2, 0, 1, 3, 2]
4. 使用reduce()
from functools import reducetest_list = [1, 4, 6, 1, 4, 5, 6]
# printing the original list
print ("The original list is : " + str(test_list))unique_list = reduce(lambda l, x: l + [x] if x not in l else l, test_list, [])res = [unique_list.index(i) for i in test_list]
# printing result
print ("The unique value list is : " + str(res))
输出
The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 1, 2, 0, 1, 3, 2]
5. 使用sorted()和bisect_left()
import bisect# initializing list
test_list = [1, 4, 6, 1, 4, 5, 6]# printing the original list
print ("The original list is : " + str(test_list))# assign unique value to list elements using sorted() and bisect_left()
sorted_list = sorted(test_list)
res = []
for i in test_list:idx = bisect.bisect_left(sorted_list, i)res.append(idx)# printing result
print ("The unique value list is : " + str(res))
输出
The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 2, 5, 0, 2, 4, 5]
6. 使用numpy
import numpy as nptest_list = [1, 4, 6, 1, 4, 5, 6]
# printing the original list
print("The original list is:", test_list)# convert list to numpy array
arr = np.array(test_list)# get unique values and their indices
unique_arr, unique_indices = np.unique(arr, return_inverse=True)# get indices of unique values for each element in original list
res = unique_indices.tolist()# printing result
print("The unique value list is:", res)
输出
The original list is: [1, 4, 6, 1, 4, 5, 6]
The unique value list is: [0, 1, 3, 0, 1, 2, 3]