Skip to content

Instantly share code, notes, and snippets.

@friveroll
Last active November 20, 2022 01:02
Show Gist options
  • Save friveroll/d2316669de4a9d5ce23acc04569b7806 to your computer and use it in GitHub Desktop.
Save friveroll/d2316669de4a9d5ce23acc04569b7806 to your computer and use it in GitHub Desktop.
Agrupa en una lista de listas los archivos que tienen el mismo nombre pero distinta extensión con ayuda de una expresión regular para archivos que tengan hasta 5 palabras en el nombre separadas por espacios
""" Group files by extension
https://es.stackoverflow.com/questions/569309/c%c3%b3mo-hacer-una-lista-de-listas-apartir-de-una-lista-segun-una-condici%c3%b3n-dada-po/569354
"""
import re
lst = [
"Iris.pdf",
"Orchids.csv",
"Rose.csv",
"Iris.txt",
"Lavender.csv",
"Lily.csv",
"Iris.csv",
"Carnations.csv",
"Rose.txt",
"Rose.pdf",
]
def file_list(list_):
"""Return a list of list of files with the
same name but different extensions
:param list_: list of files with different filenames
and differenty extensions
"""
get_filename_expr = r"(.*?)\.([^\.]*)$"
names = {
re.search(get_filename_expr, filename)[1] for filename in list_
}
return [re.findall(rf"{name}\.\w+", " ".join(lst)) for name in names]
if __name__ == "__main__":
print(file_list(lst))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment