1
# 无集合set
list_ = ['a','a','b','b','c','c']
list_new = []
for i in list_:
if i not in list_new:
list_new.append(i)
list_new
def fun_1(list_ = ['a','a','b','b','c','c']):
list_new = []
for i in list_:
if i not in list_new:
list_new.append(i)
return list_new
fun_1()
2
# 利用集合set
list_ = ['a','a','b','b','c','c']
list_new = []
set_ = set()
for i in list_:
if i not in set_:
list_new.append(i)
set_.add(i)
list_new
def fun_2(list_ = ['a','a','b','b','c','c']):
list_new = []
set_ = set()
for i in list_:
if i not in set_:
list_new.append(i)
set_.add(i)
return list_new
fun_2()
3
# 无新列表
def fun_3(list_ = ['a','a','b','b','c','c']):
set_ = set()
for i in list_:
if i not in set_:
yield i
set_.add(i)
ob = fun_3()
list_new = []
try:
while 0<1:
list_new.append(ob.__next__())
except:
print(list_new)
ob.__next__()
[ i for i in fun_3() ]
ob = fun_3()
list_new = []
for i in ob:
list_new.append(i)
list_new
list_new = []
for i in fun_3():
list_new.append(i)
list_new
标签:set,重后,list,列表,_.,new,fun,元素,append
From: https://blog.51cto.com/u_16055028/6505012