python实现列表去重的方法
#1.内置函数set: list1=[1,2,1,2,3,4,5,6] list2=list(set(list1)) print(list2) #遍历去重 list1=[1,2,1,2,3,4,5,6] list2=[] for i in list1: if not i in list2: list2.append(i) print(list2) #列表推到式 list1=[1,2,1,2,3,4,5,6] list2=[] print([list2.append(i) for i in list1 if not i in list2])
将a=[[1,2],[3,4],[5,6]]得到[1,2,3,4,5,6]
a=[[1,2],[3,4],[5,6]] list1=[j for i in a for j in i ] print(list1)
如何用Python输出一个Fibonacci数列?
def fib(num): result=[0,1] for i in range(num-2): result.append(result[-2]+result[-1]) return(result) n=int(input('输入fibs多少项:')) print(fib(n))