A 3-level nested list m
m=["a",["b","c",["inner"]]]
needs parsing For the basic data items a, b, c, inner
Basic method of getting data items:
for i in m:
print i This can only take out the first level a, and combine it with a 2-level nested list ["b", "c", ["inner"]]
Combined with the built-in Functions and judgments can continue to parse this 2-level list
for i in m: if isinstance(i,list): for j in i: print j else: print i结果 a b c ['inner']
This 2-level nesting is also separated, but the list inside is not split, although it can continue The result is obtained after disassembly, but it is not the best choice
Constructor, iteratively parse this multi-layer nested list
def printm(listin): for i in listin: if isinstance(i,list): printm(i) else: print i使用该函数直接解析嵌套列表,一次拆完 printm(m)
The result As follows:
a b c inner
The above comprehensive analysis of Python-nested list list is all the content shared by the editor. I hope it can help It is a reference for everyone, and I hope everyone will support the PHP Chinese website.
For more articles related to Python-nested list parsing, please pay attention to the PHP Chinese website!