a =(1,2,2,1)print("去重元组",set(a))# 去重元组#
a =[1,2,3,2]print("去重列表",set(a))# 去重列表#
a ='123123'print("去重字符串",set(a))# 去重字符串
通过字典的key去重 因为字典的key是不可以重复的,所以我们就可以通过字典的key来进行去重.
a =[1,2,3,1,2]
dict1 ={}for i in a:
dict1[i]=''print("通过字典的可以来进行去重", dict1.keys())
通过python内置的函数来去重 我们可以使用reduce这个方法来进行去重 官方的一段介绍 Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. 加粗样式
a =[1,2,3,4,1,2,3,4,5,6,7]deffun1(a, b):ifnotstr(b)in a:
a.append(str(b))return a
c =reduce(fun1,[[],]+ a)print(c)