码迷,mamicode.com
首页 > 其他好文 > 详细

有趣习题集

时间:2020-01-04 10:48:05      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:type   int   for   def   list   调用   循环   列表   func   


1.若给定一个列表a = [1, 2, [3, 4, [5, [6, 7, [8, [9, 10]]]]]],如何取到里面的每一个数
思路:可以用递归(自身调用)
a = [1, 2, [3, 4, [5, [6, 7, [8, [9, 10]]]]]]
def func(x):
    for i in x:
        if type(i) is list: # 判断循环的结果是否是列表,若是则在循环一次
            func(i)
        else:
            print(i)


func(a)

有趣习题集

标签:type   int   for   def   list   调用   循环   列表   func   

原文地址:https://www.cnblogs.com/jiangxianseng/p/12148054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!