def func1(one_list):
'''
使用集合,个人最常用
'''
return list(set(one_list))
def func2(one_list):
'''
使用字典的方式
'''
return {}.fromkeys(one_list).keys()
def func3(one_list):
'''
使用列表推导的方式
'''
temp_list = []
for one in one_list:
if one not in temp_list:
temp_list.append(one)
return temp_list
def func4(one_list):
'''
使用排序的方法
'''
result_list = []
temp_list = sorted(one_list)
i = 0
while i < len(temp_list):
if temp_list[i] not in result_list:
result_list.append(temp_list[i])
else:
i += 1
return result_list
def func5(two_list):
'''
二元数组去重
:param two_lsit:
:return:
'''
temp_list = []
for one in two_list:
if one not in temp_list:
temp_list.append(one)
return temp_list
def func6(three_list):
temp_list = []
for one in three_list:
if one not in temp_list:
temp_one = []
for i in one:
if i not in temp_one:
temp_one.append(i)
temp_list.append(temp_one)
# two = {}.fromkeys(one).keys()
# if two not in temp_list:
# temp_list.append(one)
return temp_list
if __name__ == '__main__':
one_list = [56, 7, 4, 23, 56, 9, 0, 56, 12, 3, 56, 34, 45, 5, 6, 56]
print(func1(one_list))
print(func2(one_list))
print(func3(one_list))
print(func4(one_list))
print("-----------------------------------------")
two_list = [['a','b'],[1,'a'],[1,'a']]
print(func5(two_list))
print("-----------------------------------------")
three_list = [['a', 'b', 'a', 'b'], [1, 'a'], ['a',1] ,[1, 'a'] ]
print(func6(three_list))
结果:
[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]
dict_keys([56, 7, 4, 23, 9, 0, 12, 3, 34, 45, 5, 6])
[56, 7, 4, 23, 9, 0, 12, 3, 34, 45, 5, 6]
[0, 3, 4, 5, 6, 7, 9, 12, 23, 34, 45, 56]
-----------------------------------------
[['a', 'b'], [1, 'a']]
-----------------------------------------
[['a', 'b'], [1, 'a'], ['a', 1]]
最后一种,因为list是有序的,所以[1, ‘a’], [‘a’, 1]],是两种,如果大家有什么可以解决方法,可以评论留言。