递归女神,你怎么看?

Python学习入门基础教程(learning Python)--4.2.4 Python的for递归实现     

呵呵

     那,如何实现上一节提出的用递归的思想将不知道有多少层list的列表里的数据一条条的输出呢?参考递归女神(递归妹子)的编程指导思想,结合伟大的三个代表精神和和谐发展观,终于在党的领导下我实现了在Python里用for循环递归打印所有数据的伟大历史任务!

     为了便于理解,先看看有3层、4层嵌套的list是如何实现数据一一输出打印的(没有递归呦),

    1. 两层嵌套的list列表

 

def p():
        for i in [1, 2, [3, 4], 5, 6, [7, 8, 9]]:
                if isinstance(i, list):
                        for j in i:
                                print j
                else:
                        print i
def main():
        p()

main()


 

     2. 三层嵌套的list列表

 

def p():
        for i in [1, 2, [3, 4], 5, 6, [7, [8, 9]]]:
                if isinstance(i, list):
                        for j in i:
                                if isinstance(j, list):
                                        for k in j:
                                                print k
                                else:
                                        print j
                else:
                        print i
def main():
        p()

main()


        3. 四层嵌套的list列表

 

 

def p():
        for i in [1, 2, [3, 4], 5, 6, [7, [8, [9, 0]]]]:
                if isinstance(i, list):
                        for j in i:
                                if isinstance(j, list):
                                        for k in j:
                                                if isinstance(k, list):
                                                        for l in k:
                                                                print l
                                                else:
                                                        print k
                                else:
                                        print j
                else:
                        print i
def main():
        p()

main()    

 


      4. 五层嵌套的list列表

 

def p():
        for i in [1, 2, [3, 4], 5, 6, [7, [8, [9, 0]]]]:
                if isinstance(i, list):
                        for j in i:
                                if isinstance(j, list):
                                        for k in j:
                                                if isinstance(k, list):
                                                        for l in k:
                                                                if isinstance(l, list):
                                                                        for m in l:
                                                                                print m
                                                                else:
                                                                        print l
                                                else:
                                                        print k
                                else:
                                        print j
                else:
                        print i
def main():
        p()

main()



      n层怎么办?找上边两个程序规律,即共同点。

 

     用递归的思想将不知道有多少层嵌套list的list列表输出打印。

5. 递归实现n层嵌套list列表

     

     程序代码如下所示。

 

li =  [1, 2, [3, 4], 5, 6, [7, [8, [9, 0]]]]
def recursion(l):
	for i in l:
		if isinstance(i, list):
			recursion(i)
		else:
			print i
def main():
	recursion(li)

main()

     程序运行结果如下图所示。用递归是不是很强大? 递归妹子、递归女神,你怎么看?

 


    未完,待续!To be continued


 

posted @ 2013-07-01 21:00  爱生活,爱编程  阅读(323)  评论(0编辑  收藏  举报