Python教程

python面试题目(二)

本文主要是介绍python面试题目(二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

转自:https://www.weidianyuedu.com/

方法一:

List=[‘b’,‘b’,‘d’,‘b’,‘c’,‘a’,‘a’]
print “the list is:” , List
if List:
List.sort()
last = List[-1]
for i in range(len(List)-2, -1, -1):
if last==List[i]:
del List[i]
else:
last=List[i]
print "after deleting the repeated element the list is : " , List
方法二:使用列表综合

l1 = [‘b’,‘c’,‘d’,‘b’,‘c’,‘a’,‘a’]
l2 = []
[l2.append(i) for i in l1 if not i in l2]
print l2
题目三:实现斐波那契(Fibonacci)数列
方法一:递归
def fibonacci2(n):
if n == 1 or n == 2:
return 1
else:
return fibonacci2(n-1) + fibonacci2(n-2)
方法二:迭代

def fibonacci(n):
if n == 1 or n == 2:
return 1

nPre = 1  
nLast = 1  
nResult = 0  
i = 2  
while i < n:  
    nResult = nPre + nLast  
    nPre = nLast  
    nLast = nResult  
    i += 1  

return nResult  

print fibonacci(5)

这篇关于python面试题目(二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!