1.新列表去重
def remove_same(list):
list.sort() # 不需要重新排序的话删除此句即可
new_list = []
for i in list:
if i not in new_list:
new_list.append(i)
return new_list
lists = [1,3,2,4,9,2,3,5,6,7,10,66,23,33,4,2]
remove_same(lists)
2.pop删除
def remove_same(list):
list.sort() # 不需要重排序删除此句即可
# new_list = []
for i in list:
if lists.count(i) >= 2:
list.pop(list.index(i)) # pop()参数是元素的索引
return list
lists = [1,3,2,4,9,2,3,5,6,7,10,66,23,33,4,2]
remove_same(lists)
3.set删除
注:set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
def remove_same(list):
new_list = set(list)
return new_list
lists = [1,3,2,4,9,2,3,5,6,7,10,10,66,23,33,4,2,3]
remove_same(lists)