Python_ nested lists into normal lists

Source: Internet
Author: User

How to put [1, 5, 6, [2, 7, [3, [4, 5, 6]]] into [1, 5, 6, 2, 7, 3, 4, 5, 6]?

Thinking:

--The For loop iterates through the list layer at a time

--Add a single value to the new list

--To change the nested list to a new traversal list, you need to nest a while loop outside of the For loop

--When the innermost list is nested, the last value is processed

#!/usr/bin/python3__author__ = ' Beimenchuixue ' __blog__ = ' http://www.cnblogs.com/2bjiujiu/' def change_l (raw_l): median_l = raw_l # defines recursive receive nested list new_l = [] # define receive fetch data                                     The empty list count = 0 # loop count, used for loop list length count while True:                # start Loop try:for i in median_l: # print one layer per loop count + = 1 If Count < Len (median_l): # Calculates the length less than the initial list and takes out a layer of data new_l.append (i) elif cou NT = Len (median_l): # When the count length is equal to the initial list length, remove the two-level nested list median_l = i # To change the initial list to the top out of the nested                      Nested list, which becomes the two-level nested count = 0 # that iterates through the initial list 0 except Exception as e: # Exception Printing exception print (e) print (new_l) # Print every time the value of the added PRI is traversed                   NT (median_l)          # Print nested list Try:len per fetch (median_l) # If there is only one value at a time, the Len () exception is triggered            Except TypeError as E:print (e) new_l.append (median_l) # Add the last value in Break # Add the last value to jump out of the loop return new_lif __name__ = = ' __main__ ': raw_l = [1, 5, 6, [                       2, 7, 7, [3, [4, 5, 6]]] # defines an initial nested list new_l = change_l (raw_l=raw_l) print (' change_l: ', new_l) # Print a well-processed list

Did not solve a problem:

--The problem itself is very special, a bit recursive nesting, unable to solve a layer of more than 2 nested lists

How to solve [' A ', ' B ', 1, [' C ', [2, ' d '], 3, 4, 5, [' E ', 6, ' f ', ' e '], 7], 8] Become a regular list?

   logical collation   
Span style= "font-family:"microsoft yahei"; Font-size:18px "> Essentially through the For loop attribute, the For loop can only traverse one layer of
by traversing, the individual values are taken out, the matching requirements are added to the new list, and the non-conforming add to the intermediate list
The hardest part is, how to judge the last cycle?
my idea is

If the intermediate list equals the number of exceptions, The description loops to the last list, exiting the entire loop

#!/usr/bin/python3__author__ = ' Beimenchuixue ' __blog__ = ' http://www.cnblogs.com/2bjiujiu/' def change_l (raw_l): New_ L = [] # initial result list median_l = [] # loops the nested list received, an intermediate list W Hile true:for i in Raw_l:try:if len (i): # Determines if the check out is an integer, is an integer trigger exception, nested list and                            The string has a length of try:if i.isalnum (): # Determines whether a number or letter is taken out, not a number or letter triggering an exception                        New_l.append (i) # is a number or letter added to List_a except Exception as E: # trigger is not a number or letter exception  Print (e) median_l.extend (i) # Add the removed nested list to the median_l raw_l =              Median_l # Loop raw_l point to median_l Intermediate list print (raw_l) except Exception as E: # trigger Integer len () method exception Print (e) new_l.append (i) # is an integer added to new_l # to determine the most Nested list in the following nested list Count = 0 for I in median_l: # loop Two-layer nested list try: # trying to determine the last layer nested                     Nested list, if nested, the number of exceptions is less than the list length len (i) # integer triggering exception i.isalnum ()                      # not a numeric or alphabetic type string triggering exception except Exception as E:print (e) Count + = 1            # Each occurrence of an exception, the number of exceptions plus 1 if count = = Len (median_l): # Determine if the number of exceptions is equal to the last loop list length, if equal, confirm that the last layer of the list has been looped, exit the entire loop Break median_l = [] # Place a list in the air, receive the next level of nested list return new_lif __name__ = = ' __main__ ': raw_l = [' A ', ' B ', 1, [' C ', [2, ' d '], 3, 4, 5, [' E ', 6, ' f ', ' e '], 7], 8, ' G '] # Initial normal nesting list new_l = change_l (raw_l ) Print (new_l)

  

Python_ nested lists into normal lists

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.