标签:列表操作 code color lse func ram lang 定义 实现
count = 1
def xiake():
global count
print(count)
print(‘下课‘)
count+=1
xiake()
xiake()
lst = [‘a1‘,‘a2‘,[‘a11‘,‘a22‘,[‘b11‘,‘b22‘,[‘ccc‘,‘ddd‘]]]]
def look_up(lst): # [‘a1‘,‘a2‘,[‘a11‘,‘a22‘,[‘b11‘,‘b22‘,[‘ccc‘,‘ddd‘]]]]
for i in lst:
if type(i) is list: # [‘a11‘,‘a22‘,[‘b11‘,‘b22‘,[‘ccc‘,‘ddd‘]]]
look_up(i) # lst([‘a11‘,‘a22‘,[‘b11‘,‘b22‘,[‘ccc‘,‘ddd‘]]])
else:
print(i) # ‘a1‘ ‘a2‘
look_up(lst)
l1 =[1,[90,80.2],4,[5,6,7,8,[6,3,2]]] # 求列表中所有元素的和
def rec_sum(lst):
sum_n = 0
for n in lst:
if type(n) is list:
ret = rec_sum(n)
sum_n += ret
elif type(n) is int or type(n) is float:
sum_n += n
return sum_n
ret1= rec_sum(l1) # [1,[90,80.2],4,[[6,3,2],5]]
print(ret1) 结果:212.2
def fn(n): # n=3
if n>1:
return n * fn(n-1)
else:
return n
ret = fn(10)
print(ret) 结果:3628800
标签:列表操作 code color lse func ram lang 定义 实现
原文地址:https://www.cnblogs.com/MLing/p/12850881.html