栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python中os.walk的时间复杂度

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python中os.walk的时间复杂度

好吧…让我们浏览一下源代码:)

文件:http :
//docs.python.org/2/library/os.html#os.walk

def walk(top, topdown=True, onerror=None, followlinks=False):    islink, join, isdir = path.islink, path.join, path.isdir    try:        # Note that listdir and error are globals in this module due        # to earlier import-*.        # Should be O(1) since it's probably just reading your filesystem journal        names = listdir(top)    except error, err:        if onerror is not None: onerror(err)        return    dirs, nondirs = [], []    # O(n) where n = number of files in the directory    for name in names:        if isdir(join(top, name)): dirs.append(name)        else: nondirs.append(name)    if topdown:        yield top, dirs, nondirs    # Again O(n), where n = number of directories in the directory    for name in dirs:        new_path = join(top, name)        if followlinks or not islink(new_path): # Generator so besides the recursive `walk()` call, no additional cost here. for x in walk(new_path, topdown, onerror, followlinks):     yield x    if not topdown:        yield top, dirs, nondirs

因为它是一个生成器,所以这全都取决于您走树的距离,但是看起来给定路径中文件/目录的总数

O(n)
在哪里
n



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号