For multi-list nesting, how do I get all the elements?
Before using the For loop +if to determine, decomposition, as follows:
For First_layer in lists:
If Isinstance (first_layer,list):
For Second_layer in First_layer:
If Isinstance (second_layer,list):
For Third_layer in Second_layer:
Print Third_layer
Else
Print First_layer
However, if you encounter a list with 4 nested, 5 nested ... And so on, the code looks incredibly complex and bloated.
So I think of the function reusability, as follows:
#!/usr/bin/env python
#coding: Utf-8
Fruit=[' A ', ' B ', 123,[' C ', 345, ' d ', [' e ', ' f ', 90],22, ' cc '], ' po ', 34]
def layer (lists):
For First_layer in lists:
If Isinstance (first_layer,list):
Layer (First_layer)
Else
Print First_layer
Layer (fruit)
In this way, no matter how many lists are nested, simply pass the argument to the function on the line, you can achieve a stepwise decomposition. Functions can also be ported for invocation and usability enhancements.
This article is from the "Snail's Home" blog, be sure to keep this source http://winters.blog.51cto.com/5617866/1619684
python-decomposition multi-list nesting