不再怀疑

导航

《head first python》第一章《初识python》总结

  该章主要介绍了python的安装和列表相关  

  列表主要用了movies做介绍:

  1.列表及嵌套列表:movies=["the hodaliy",1975,"zhangyimou",2012,["liangchaowei",["jinchengwu","gpngli","wangzhuang","zhaoyouyt","wangwei"]]]

  2.嵌套列表的层级打印:{print (movies[4]);['liangchaowei', ['jinchengwu', 'gpngli', 'wangzhuang', 'zhaoyouyt', 'wangwei']]}

             { print (movies[4][1][3]);zhaoyouyt}

  3.for循环,while循环,以及if else和isinstance(xxx,list)

    用 for循环和if else 以及isinstance来打印出嵌套的元素

  for each_item in movies:
    if isinstance (each_item,list):
        for new_item in each_item:
            print(new_item)
    else:
        print(each_item)

 

  4.用def 嵌套打印出每一个元素

  def print_lol(the_list):
    for each_item in the_list:
        if isinstance(each_item,list):
            print_lol(each_item)
        else:
            print(each_item)

 

  调用函数:

   print_lol(movies)

  打印成功:

the hodaliy
1975
zhangyimou
2012
liangchaowei
jinchengwu
gpngli
wangzhuang
zhaoyouyt
wangwei

 

posted on 2017-01-08 21:36  不再怀疑  阅读(143)  评论(0编辑  收藏  举报