Cyclic output python isinstance () function for nested lists

Requirements: output list [ "dd", "sd" , "sadf", [ "as", "sd", "aa"]] each element, if they list, the input list element nesting The output result
dd
SD
SADF
aS
SD
AA

Idea: use a for loop iterates through the list can be achieved, but the overall output will be nested lists. So, consider joining if statement to determine whether the list traversal, and if so, output cycle again.
It determines whether the list can be used
isinstacn (STR, List)
STR: To determine whether an object is a
list: Data Type

code show as below:

s=["dd","sd","sadf",["as","sd","aa"]]
for i in s: #第一层遍历
    if isinstance(i,list):    #判断是否为列表,如果是,进入内层循环
        for j in i:
            print(j)
    else:       #如果不是列表,直接打印
        print(i)
Published 13 original articles · won praise 1 · views 200

Guess you like

Origin blog.csdn.net/aa12551827/article/details/104367759
Recommended