|
10鱼币
各位鱼油大家好,前两天在checkio(https://py.checkio.org/)上看到一道题,结果转了好几个圈,还是没能转出来,反而把自己给绕晕了。。
原题如下:
Sort by Extension
You are given a list of files. You need to sort this list by the file extension. The files with the same extension should be sorted by name.
Some possible cases:
Filename cannot be an empty string;
Files without the extension should go before the files with one;
Filename ".config" has an empty extension and a name ".config";
Filename "config." has an empty extension and a name "config.";
Filename "table.imp.xls" has an extension "xls" and a name "table.imp";
Filename ".imp.xls" has an extension "xls" and a name ".imp".
Input: A list of filenames.
Output: A list of filenames.
Example:
- sort_by_ext(['1.cad', '1.bat', '1.aa']) == ['1.aa', '1.bat', '1.cad']
- sort_by_ext(['1.cad', '1.bat', '1.aa', '2.bat']) == ['1.aa', '1.bat', '2.bat', '1.cad']
- sort_by_ext(['1.cad', '1.bat', '1.aa', '.bat']) == ['.bat', '1.aa', '1.bat', '1.cad']
- sort_by_ext(['1.cad', '1.bat', '.aa', '.bat']) == ['.aa', '.bat', '1.bat', '1.cad']
- sort_by_ext(['1.cad', '1.', '1.aa']) == ['1.', '1.aa', '1.cad']
- sort_by_ext(['1.cad', '1.bat', '1.aa', '1.aa.doc']) == ['1.aa', '1.bat', '1.cad', '1.aa.doc']
复制代码
翻译:
按扩展名排序
您将获得一份文件列表。您需要按文件扩展名对列表进行排序。具有相同扩展名的文件应该按名称排序。
一些可能的情况:
文件名不能是空字符串;
没有扩展名的文件应该放在有扩展名的文件之前;
文件名 “.config” 有一个空的扩展名和名称 “.config”;
文件名 “config.” 扩展名为空,名称为 “config”;
文件名 “table.imp.xls” 的扩展名为 “xls”,名称为 “table.imp”。
文件名 " imp.xls " 有一个扩展名 " xls " 和一个名称 “imp”。
输入:文件名列表。
输出:文件名列表。
示例:
- sort_by_ext(['1.cad', '1.bat', '1.aa']) == ['1.aa', '1.bat', '1.cad']
- sort_by_ext(['1.cad', '1.bat', '1.aa', '2.bat']) == ['1.aa', '1.bat', '2.bat', '1.cad']
- sort_by_ext(['1.cad', '1.bat', '1.aa', '.bat']) == ['.bat', '1.aa', '1.bat', '1.cad']
- sort_by_ext(['1.cad', '1.bat', '.aa', '.bat']) == ['.aa', '.bat', '1.bat', '1.cad']
- sort_by_ext(['1.cad', '1.', '1.aa']) == ['1.', '1.aa', '1.cad']
- sort_by_ext(['1.cad', '1.bat', '1.aa', '1.aa.doc']) == ['1.aa', '1.bat', '1.cad', '1.aa.doc']
复制代码
自己的代码改了不下 20 遍,写了两天。。愣是没做出来。。只好厚着脸皮在这里向各位请教了
- import re
- def checklist(filelist): #判断是否有空字符
- for file in filelist:
- if file == '':
- return False
- return True
- def sort_by_ext(filelist):
- if not checklist(filelist):
- raise ValueError('传入的文件名列表不能存在空字符!')
-
- pattern = re.compile(r'(.*)\.(.*)')
- filename = []
- extension = []
- for file in filelist:
- r = pattern.search(file)
- if r.group(1) != '':
- filename.append(r.group(1))
- extension.append(r.group(2))
- else:
- filename.append('.' + r.group(2))
- extension.append('')
-
- length = len(filename)
- file_dict={}
- for i in range(length):
- if extension[i] not in file_dict:
- file_dict[extension[i]] = [filename[i]]
- else:
- file_dict[extension[i]].append(filename[i])
- sort_ext = sorted(file_dict.keys())
- new_file_dict = {}
- for each in sort_ext:
- file_dict[each].sort()
- new_file_dict[each] = file_dict[each]
-
- result = []
- for i in new_file_dict.keys():
- for j in new_file_dict[i]:
- result.append(j + '.' + i)
-
- print(result)
复制代码
不确定是否有遗漏。。
|
|