zoukankan      html  css  js  c++  java
  • 609. 在系统中查找重复文件





    代码思路:用文件内容作key域,value域需要拼接,是文件全名。

    import re
    class Solution(object):
        def findDuplicate(self, paths):
            """
            :type paths: List[str]
            :rtype: List[List[str]]
            """
            mydict = {}
            for item in paths:
                temp = item.split()
                # 获取当前路径
                path = temp[0]
                # 获取当前路径下的所有文件
                files = temp[1:]
                # 遍历每一个文件,取文件内容作为key,往mydict里存
                for file in files:
                    # 用正则表达式获取文件内容
                    fileContextList = re.findall(r'[(](.*?)[)]', file)
                    fileContextStr = ''.join(fileContextList)
                    extraSize = len(fileContextStr) + 2
                    fileName = list(file)[:-extraSize]
                    if fileContextStr in mydict.keys():
                        mydict[fileContextStr].append(path + "/" + ''.join(fileName))
                    else:
                        mydict[fileContextStr] = [path + "/" + ''.join(fileName)]
            res = []
            for item in mydict.values():
                if len(item) > 1:
                    res.append(item)
            return res
    
  • 相关阅读:
    os模块
    函数练习
    集合 去重
    作业二:购物车程序
    作业一: 三级菜单
    字典练习
    字典
    切片
    冒泡练习
    判断整型数据奇偶数
  • 原文地址:https://www.cnblogs.com/panweiwei/p/14024900.html
Copyright © 2011-2022 走看看