pythonPython语言与信息数据获取和机器学习机器学习

python 操作简记 (递归查找制定后缀文件)(list排序字

2017-10-09  本文已影响27人  ciantian

递归查找制定后缀的文件

用到三个库

def recursiveSearchFiles(dirpath, keywords):
    fileList = []
    pathList = glob.glob(os.path.join(dirpath, '*'))
    for mPath in pathList:
        # fnmatch 用于匹配后缀
        if fnmatch.fnmatch(mPath, keywords):
            fileList.append(mPath)  # 符合条件条件加到列表
        elif os.path.isdir(mPath):
            fileList += recursiveSearchFiles(mPath, keywords)  # 将返回的符合文件列表追加到上层
        else:
            pass
    return fileList
if __name__ == '__main__':
    path = recursiveSearchFiles("/home/tianchaoxiong/LinuxData/data/verifies/ceshi/", "*.jpg")
    print(path)

list 转置

排序函数sorted

  1. 数字排序
    list1 = [36, 5, -12, 9, -21]
    list2 = sorted(list1)
    print(list2)

[-21, -12, 5, 9, 36]

  1. 数字按照绝对值排序
    list1 = [36, 5, -12, 9, -21]
    list2 = sorted(list1,key=abs)
    print(list2)

[5, 9, -12, -21, 36]

  1. 数字从大到小
    list1 = [36, 5, -12, 9, -21]
    list2 = sorted(list1, reverse=True)
    print(list2)

[36, 9, 5, -12, -21]

  1. 字符串排序
    list1 = ['bob', 'about', 'Zoo', 'Credit']
    list2 = sorted(list1)
    print(list2)

['Credit', 'Zoo', 'about', 'bob']

  1. 字符串忽略大小写排序
    list1 = ['bob', 'about', 'Zoo', 'Credit']
    list2 = sorted(list1, key=str.lower)
    print(list2)

['about', 'bob', 'Credit', 'Zoo']

  1. 字典排序
    list1 = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
    dict1 = dict(list1)
    # 后面的下表0 表示关键字 1表示value
    dict2 = sorted(dict1.items(), key=lambda d: d[0])  
    print(dict2)

[('Adam', 92), ('Bart', 66), ('Bob', 75), ('Lisa', 88)]

上一篇下一篇

猜你喜欢

热点阅读