Write python code to determine list depth

Source: Internet
Author: User

Mainly for nested list issues. List set list, which is deeper than the sub-list...

This problem is annoying. If you nest 100 million lists, do you want to count 10000 and then sort them?


Finally, I tried to use the extend function of list and the recursive function. The Code is as follows:


L1 = [1, 'A', [1], [, [, [, [], [, [], [4], [5], [6] # l1 = [1, 2, [3, [4, 5], 6, [7, 8, [9, 10], 11], 12], 13] count = 1def func (l): global count temp_list = [] for l2 in l: if isinstance (l2, list) = True: temp_list.extend (l2) if len (temp_list)> 0: count + = 1 func (temp_list) func (l1) print count


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.