Python遍历嵌套List方法

脑子不太好想记下点东西,主要实现的关键在于last_menucount1last_menu存储进入下一个list之前li的值,count1存储进入下一个liist之前,li的下一个位置的索引.


#!/bin/python3
# -*- coding:utf-8 -*-
li = ["1", '2', ["21", ["221", "222"], '23'], 3, 4]
count = 0
last_menu = []
count1 = []
while True:
    if count > len(li) - 1:
        if len(last_menu):
            li = last_menu[-1]
            last_menu.pop()
            count = count1[-1]
            count1.pop()
        else:
            break
    if isinstance(li[count],list):
        last_menu.append(li)
        count1.append(count + 1)
        li = li[count]
        count = 0
        continue
    print(li[count])
    count += 1

One thought on “Python遍历嵌套List方法”

Leave a Reply

Your email address will not be published. Required fields are marked *