美文网首页
python使用递归遍历嵌套列表

python使用递归遍历嵌套列表

作者: 名字好难想随便叫 | 来源:发表于2020-10-20 16:58 被阅读0次

    用isinstance()判断是否为列表:

    lists = ['ab' , 123 , [1] , ['v'] , 'dd' , [[1] , [12333]]]
    def isList(items):
        if not isinstance(items , list):
            #不要用return ,不然执行为None
            print(items)
        else :
            for item in items:
                isList(item)
    
    isList(lists)
    

    执行结果:

    ab
    123
    1
    v
    dd
    1
    12333
    

    相关文章

      网友评论

          本文标题:python使用递归遍历嵌套列表

          本文链接:https://www.haomeiwen.com/subject/lvbdmktx.html