題目一:python中String型別和unicode什麼關係
整理答案:string是位元組串,而unicode是一個統一的字符集,utf-8是它的一種儲存實現形式,string可為utf-8編碼,也可編碼為GBK等各種編碼格式
題目二:不用set集合方法,去除列表中的重複元素
方法一:
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)