Python Advanced Section Essence--those books won't tell you the pit

Source: Internet
Author: User
Tags generator

Recursive directory Generator mode, else in the TMP display to obtain yield is indispensable, in the recursive algorithm to use the generator, you need to explicitly get all yield values in the generator's original function (first call)
def get_file_recur(path):    children = os.listdir(path)    for child in children:        qualified_child = os.path.join(path,child)        if os.path.isfile(qualified_child):            yield qualified_child        else:            tmp = get_file_recur(qualified_child)            for item in tmp:                yield itemfor file in get_file_recur('/home/xxx/xxx/Frank Li'):    print(file)
Reference sources, as follows Flattern list
def flattern(lst):    for item in lst:        if isinstance(item,list):            inner_list = flattern(item)            for i in inner_list:                yield i        else:            yield iteml=[1,2,3,4,5,[6],[7,8,[9,[10]]]]lst=flattern(l)print(list(lst))

Python Advanced Section Essence--those books won't tell you the pit

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.