先随机生成一个有重复元素的列表
l = [random.randint(1,3) for i in range(10)]
l
[2, 2, 3, 1, 1, 1, 2, 3, 2, 3]
接下来,开始去重。
1.set()
def func_set():
return list(set(l))
func_set()
[1, 2, 3]
2.fromkeys()
def func_fromkeys():
l = list(dict.fromkeys(l).keys())
return l
func_fromkeys()
[2, 1, 3]
def func():
l1 = []
for i in l:
if i not in l1:
l1.append(i)
return l1
func()
[3, 1, 2]