Python 正则表达式和模式匹配内部列表

Python 正则表达式和模式匹配内部列表,python,list,Python,List,我在当前工作目录中有一个文件名列表 my_list = ["apple.txt","mango.txt", "grapes.txt","draw.png" , "hello123.txt" , "figure.png"] 现在我想创建一个新列表,它只存储*.txt文件: new_list = ["apple.txt","mango.txt", "grapes.txt", "hello123.txt"] 有没有办法在Python中使用正则表达式和模式匹配来实现这一点。您可以使用以下方法: ne

我在当前工作目录中有一个文件名列表

my_list = ["apple.txt","mango.txt", "grapes.txt","draw.png" , "hello123.txt" , "figure.png"]
现在我想创建一个新列表,它只存储
*.txt
文件:

new_list = ["apple.txt","mango.txt", "grapes.txt", "hello123.txt"]
有没有办法在Python中使用正则表达式和模式匹配来实现这一点。

您可以使用以下方法:

new_list = [name for name in my_list if name.endswith('.txt')]
您可以使用以下选项:

new_list = [name for name in my_list if name.endswith('.txt')]

方法1
regex

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
os

from os.path import splitext
result = [i for i in my_list if splitext(i)[1] == '.txt']
方法3
拆分

result = [i for i in my_list if i.split('.')[1] in '.txt']
输出

['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt']

方法1
regex

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
os

from os.path import splitext
result = [i for i in my_list if splitext(i)[1] == '.txt']
方法3
拆分

result = [i for i in my_list if i.split('.')[1] in '.txt']
输出

['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt']
您也可以尝试以下方法:

new_list = []
for file in my_list:
    if file.endswith(".txt"):
        new_list.append(file)
print(new_list)
输出

['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt']
更新:

您还可以使用defaultdict对所有文件进行分组,如下所示:

from collections import defaultdict

d = defaultdict(list)
for file in my_list:
    key = "." + file.split(".")[1]
    d[key].append(file)
print(d)
输出:

defaultdict(<class 'list'>, {'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']})
{'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']}
输出:

defaultdict(<class 'list'>, {'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']})
{'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']}
您也可以尝试以下方法:

new_list = []
for file in my_list:
    if file.endswith(".txt"):
        new_list.append(file)
print(new_list)
输出

['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt']
更新:

您还可以使用defaultdict对所有文件进行分组,如下所示:

from collections import defaultdict

d = defaultdict(list)
for file in my_list:
    key = "." + file.split(".")[1]
    d[key].append(file)
print(d)
输出:

defaultdict(<class 'list'>, {'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']})
{'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']}
输出:

defaultdict(<class 'list'>, {'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']})
{'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']}

模式匹配和正则表达式在这里是非常不相关的。模式匹配和正则表达式在这里是非常不相关的。只有当它的扩展长度为3时,这才是catch。我的意思是,如果扩展名
.docx
这个方法不起作用。我的观点是,这是一种硬编码方法。只有当它的扩展名为3时,这才是catch。我的意思是,如果扩展名
.docx
,这种方法不起作用。我的观点是它的硬编码方法。