a = [item for index,item in enumerate(a) if index==a.index(item)]
[1,2,3,4,5,6,7,8]
注:Python的内置函数 enumerate(index,item,索引起始值)
统计文件的行数:
count = -1for index,item in enumerate(open(filename,'r'))
count += 1
方法二:for循环
new_list = []
for i in a:
if i notin new_list:
new_list.append(i)
print(new_list)
方法三:set()
new_list = list(set(a))
方法四:lambda reduce()
func = lambda x,y:x if y in x else x+[y]
reduce(func,[[],]+a)
方法五:itertools模块
import itertools
a = [1,2,3,4,5,6,7,2,1,3,5,6,7,8]
a.sort()
new_group = itertools.groupby(a)
for index,item in new_group:
print(index)
方法六:函数
defdelRepeat(list):for i in list:
while list.count(i)>1:
del list[list.index(i)]
return list
方法七:快速排序
如下,只需要把集合修改为单值元素
import random
a = [1,2,3,4,5,6,7,2,1,3,5,6,7,8]
defqsort(a):if len(a)<2:return a
pivot_element = random.choice(a)
small = [i for i in a if i ==pivot_elelment]
large = [i forinin a if i>pivot_element]
return qsort(small)+[pivot_element]+qsort(large)
print(qsort(a))