Python 处理多层嵌套列表

>>> movies =[
    "the holy grail", 1975,"terry jones",91,
    ["graham chapman",
     ["michel palin","john cheelse","terry gilliam","eric idle","terry jones"]]]
>>> print (movies)
['the holy grail', 1975, 'terry jones', 91, ['graham chapman', ['michel palin', 'john cheelse', 'terry gilliam', 'eric idle', 'terry jones']]]


>>> for each_item in movies:
    if isinstance(each_item,list):
        for nested_item in each_item:
            if isinstance(nested_item,list):    
                for last_item in nested_item:
                    print(last_item)
            else:
                print(nested_item)
    else:
        print(each_item)

        
the holy grail
1975
terry jones
91
graham chapman
michel palin
john cheelse
terry gilliam
eric idle
terry jones
原文地址:https://www.cnblogs.com/oskb/p/4810492.html