嵌套列表的格式打印

names = ['A','B','C',['D','E'],['F']]
def print_lol(the_list,indent=False,level=0):  #
    for each_item in the_list:
        if isinstance(each_item,list):
            print_lol(each_item,indent,level+1)
        else:
            if indent:
                for tab_top in range(level):
                    print("\t",end='')
            print(each_item)
print_lol(names,True,4)  #首行缩进4格输出
A B C D E F


print_lol(names,True) 
A
B
C
	D
	E
	F
print_lol(names)

A B C D E F

  

posted @ 2020-09-06 12:29  JRS077  阅读(157)  评论(0编辑  收藏  举报