請使用遞迴方式自定Function,將list中大於5
的資料加總
print sum([1, 7, 4, 6, 8, 2]) ===> 21
def sum(list):
count=0
if len(list)==0:
return 0
else:
count=list.pop()
if count>5:
return count+sum(list)
else:
return sum(list)
print sum([1,7,4,6,8,2])
------------------------------------------------------------------------------------
以下是洋基隊投手生涯數據,請依使用者所
需的資料排列法改變報表呈現方式。
W L ERA SO
C-M Wang 49 18 3.75 240
Andy Pettitte 203 114 3.83 1853
Mariano Rivera 62 44 2.34 864
list=[["c-m-wang",49,18, 3.75,240],["Andy Pettitte",203,114, 3.83 ,1853],["Mariano Rivera",62,42,2.34,864]]
s = int(raw_input('sort type'))
tmplist = []
for i in range(0,3):
for j in range(0,2-i):
if list[j][s]> list[j+1][s]:
tmplist = list[j+1]
list[j+1] = list[j]
list[j] = tmplist
for i in range(0,3):
print list[i]
-----------------------------------------------------------------------------------------------------------------
例如:
Find(‘a’, ‘cocacola’) ----> 4
Find(‘k’, ‘cocacola’) ---->not found
/////////////////////////////////////////////////////////////////////////////////////////////////
def kkk(str,str2):
if len(str2)==0:
print "not found"
else:
for i in str[i]:
temp.append(i)
kkk('a','sfjishhjhza')