Python (sequence recursion) "Output atomic level element ... 】

Source: Internet
Author: User

Go back in the evening to review the original information, return to the codebook there is a "expand a nested Sequence" topic.

Task description: The child in the sequence may be a sequence, the child of the subsequence may still be a sequence, and so on, the sequence nesting can reach any depth. You need to loop through a sequence and expand all of its subsequence into a single sequence with only the basic subkeys.

For example, the shape is the following sequence a:

a = [(123 (7" , 45, 69, 10)]],

(Figure 1), and I might have a problem understanding the atomic level elements that are described in the task. My understanding is that I want to spit out this sequence.

such as the shape of[2, 3, (7, 8)] And< Span style= "color: #cc7832;" >< Span style= "color: #cc7832;" > (9 can still be split, the result of Figure 1 is only to the second layer when the sequence is not split, so do it yourself to do this implementation, so that the final result 2 is shown:

< Span style= "font-size:15px;" >< Span style= "color: #6897bb;" >< Span style= "color: #6897bb;" > (Figure 2).

< Span style= "font-size:15px;" >< Span style= "color: #6897bb;" >< Span style= "color: #6897bb;" >

< Span style= "font-size:15px;" >< Span style= "color: #6897bb;" >< Span style= "color: #6897bb;" >

< Span style= "font-size:15px;" >< Span style= "color: #6897bb;" >< Span style= "color: #6897bb;" >

def flatten (sequence): For    i in sequence:        if Isinstance (i, (list, tuple)):            Flatten (i)            if not isinstance (I, (list, tuple)): For                subitem in I:                    print subitem        else:           print I

See this article by the way with their own ideas on the way to think about, feel that such treatment is still valuable, recorded. Convenient for later use ....

  

Python (sequence recursion) "Output atomic level element ... 】

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.