Regular expression and pattern matching inside LIST
本问题已经有最佳答案,请猛点这里访问。
我在当前工作目录中有一个文件名列表。
1 | my_list = ["apple.txt","mango.txt","grapes.txt","draw.png" ,"hello123.txt" ,"figure.png"] |
现在我想创建一个只存储
1 | new_list = ["apple.txt","mango.txt","grapes.txt","hello123.txt"] |
在Python中,有没有任何方法可以使用正则表达式和模式匹配来实现这一点。
您可以使用:
1 | new_list = [name for name in my_list if name.endswith('.txt')] |
方法1用
1 2 3 4 | import re txt_regex = re.compile(r'(\w+.txt)') my_list = ["apple.txt","mango.txt","grapes.txt","draw.png" ,"hello123.txt" ,"figure.png"] result = [i for i in my_list if txt_regex.match(i)] |
正则表达式的演示。
方法2与
1 2 | from os.path import splitext result = [i for i in my_list if splitext(i)[1] == '.txt'] |
方法3与
1 | result = [i for i in my_list if i.split('.')[1] in '.txt'] |
输出
1 | ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'] |
您也可以尝试:
1 2 3 4 5 | new_list = [] for file in my_list: if file.endswith(".txt"): new_list.append(file) print(new_list) |
产量
1 | ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'] |
更新:
还可以使用defaultdict对所有文件进行分组,如下所示:
1 2 3 4 5 6 7 | from collections import defaultdict d = defaultdict(list) for file in my_list: key ="." + file.split(".")[1] d[key].append(file) print(d) |
输出:
1 | defaultdict(<class 'list'>, {'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']}) |
或者即使没有默认的dict:
1 2 3 4 5 6 7 | d = {} for file in my_list: key ="." + file.split(".")[1] if key not in d: d[key] = [] d[key].append(file) print(d) |
输出:
1 | {'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']} |