How does python convert a nested list into a normal list?
How to change [1, 5, 6, [2, 7, [3, [4, 5, 6] to [1, 5, 6, 2, 7, 3, 4, 5, 6]?
Thoughts:
-- For Loop traverses the list layer every time
-- Adds a single retrieved value to the new list.
-- To convert the obtained nested list to a new traversal list, you need to nest A while LOOP outside the for loop.
-- Process the last value when the innermost list is nested.
#! /Usr/bin/python3 _ author _ = 'beimenchuixue '_ blog _ = 'HTTP: // www.cnblogs.com/2bjiujiu/'def change_l (raw_l ): "" This function processing list is special, the last value in the list of each layer must be the nested list "median_l = raw_l # Intermediate List new_l = [] # result list count = 0 # count the number of cycles and the length of the list while True: try: for value in median_l: # obtain a value in each for loop count + = 1 if count <len (median_l): # if the calculation is smaller than the list length, the last nested list new_l.append (value) elif count = len (median_l) is not retrieved: # When the count length is equal to the list length, retrieve the second-layer nested list median_l = value # point each time to the last nested list of each layer count = 0 # Calculate the zeroth distinct T Exception as e: # print an Exception print (e) try: len (median_l) # Try to get the length of the last value of each layer, instead of a list error, and add the last value to the result list using t TypeError: new_l.append (median_l) break # Add the last value, return new_l if _ name _ = '_ main _': raw_l = [1, 5, 6, [2, 7, 7, [3, [4, 5, 6] # define an initial nested list new_l = change_l (raw_l = raw_l) print ('change _ l: ', new_l)
One problem not solved:
-- This problem is very special. It is a little recursive nesting and cannot solve the problem of having more than two nested lists on one layer,
How to solve ['A', 'B', 1, ['C', [2, 'D'], 3, 4, 5, ['E', 6, 'F', 'E'], 7], 8] is changed to a normal list?
#! /Usr/bin/python3 _ author _ = 'beimenchuixue '_ blog _ = 'HTTP: // www.cnblogs.com/2bjiujiu/'def change_l (raw_l ): "This can cope with a list of various nested types, you can convert the multi-dimensional list to "new_l = [] # The initial result list median_l = [] # The nested list received cyclically, with an intermediate list while True: for value in raw_l: try: if len (value): # judge whether the result is an integer. if it is an integer, an exception is triggered. The nested list and string both have the length try: if value. isalnum (): # determines whether the removal is a number or letter, not a number or letter trigger Exception new_l.append (value) # It is a number or letter added to list_a to cancel t Exception as e: # Trigger is not a number or letter exception print (e) median_l.extend (value) # Add the retrieved nested list to median_l raw_l = median_l # loop raw_l pointing to median_l Intermediate List print (raw_l) failed t Exception as e: # Trigger integer len () method Exception print (e) new_l.append (value) # Add an integer to new_l # check whether there is a nested list in the last nested list count = 0 for value in median_l: # loop L2 nested list try: # Try to determine whether the last layer of nested list is nested. If nested, the number of exceptions will be less than the list length len (value) # integer trigger exception value. isalnum () # It is not a numeric or letter-type string that triggers an Exception when T Exception as e: print (e) count + = 1 # Every time an Exception occurs, increase the number of exceptions by 1 if count = len (median_l): # determine whether the number of exceptions is equal to the length of the last loop list. if the number is equal to, check that the list of the last layer has been loops, exit the entire loop break median_l = [] # Set the air room list and receive the next layer of nested list return new_l if _ name _ = '_ main __': raw_l = ['A', 'B', 1, ['C', [2, 'D'], 3, 4, 5, ['E', 6, 'F', 'E'], 7], 8, 'G'] # initial normal nested list # result new_l = change_l (raw_l) print (new_l)
Logic sorting:
1. Essentially, through the for loop feature, a for loop can traverse only one layer
2. Use traversal to determine a single value, add the values that meet the requirements to the new list, and add the non-conforming values to the intermediate list.
Biggest problem:How can we judge the final cycle?
My idea is:
A. check whether a nested list exists by traversing the Intermediate List. Compare the number of exceptions with the list length,
B. If the Intermediate List is equal to the number of exceptions, the system loops to the final list and exits the entire loop.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.