How to recursively work with multiple nested lists

Source: Internet
Author: User

Create a multi-tiered list (that is, a list of stored lists) and output list items

The following figure:

You can see that the output is only output out of the list

Of course, you can iterate through each subkey multiple times: As shown in the following figure

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Note: Isinstance (object, ClassInfo) is a built-in function of Python to determine the type of object

This is a three-tier loop, and it's too cumbersome to recycle the For loop output for many times.

For this situation, a function needs to be established, and the recursive loop output child

As shown in the following illustration:

Simply create a function print_lol (), and if the built-in function isinstance () determines that the subkey is a list, call the function print_lol () itself

The movies is then used as the parameter of the function print_lol to invoke the function output list subkey

For this nested list, you can also output one of the items by using an array subscript

Such as:

>>> print (movies[5][1][1])  
sex  
>>>

Author: csdn Blog Friends of the Yangtze River

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.