Python-full resolution of nested list and python-full resolution

Source: Internet
Author: User

Python-full resolution of nested list and python-full resolution

A layer-3 nested list m

M = ["a", ["B", "c", ["inner"]

Basic data items a, B, c, and inner must be parsed.

Basic data extraction methods:

For I in m:

Print I this can only retrieve the first layer of a, and a second layer of nested list ["B", "c", ["inner"]

Combined with built-in functions and judgment, you can continue to parse this list of 2 layers.

For I in m: if isinstance (I, list): for j in I: print jelse: print I result abc ['inner ']

This layer-2 Nesting is also separated, but the list in it is not split. Although the splitting can continue to obtain the result, 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 use this function to directly parse the nested list, remove printm (m) at a time)

The result is as follows:

Abcinner

The full analysis of the above Python-nested list is all the content shared by the editor. I hope you can give us a reference and support the house of helpers.

Related Article

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.