TransWikia.com

select partially matching filenames

Mathematica Asked by T. Rihacek on June 13, 2021

I have two lists of files with slightly different naming conventions

files1 = {"DTideal_circ_def=000um_astX=000um_astY=410um.png","DTideal_circ_def=050um_defX=000um_defY=010um.png"}

files2={"DT120_blur7_Defoc=000um_AstigX=000um_AstigY=410um.png",
"DT120_blur7_Defoc=000um_AstigX=460um_AstigY=310um.png",
"DT120_blur7_Defoc=000um_AstigX=460um_AstigY=360um.png",
"DT120_blur7_Defoc=000um_AstigX=460um_AstigY=410um.png",
"DT120_blur7_Defoc=000um_AstigX=460um_AstigY=460um.png",
"DT120_blur7_Defoc=050um_AstigX=000um_AstigY=010um.png",
"DT120_blur7_Defoc=050um_AstigX=000um_AstigY=060um.png",
"DT120_blur7_Defoc=050um_AstigX=000um_AstigY=110um.png",
"DT120_blur7_Defoc=050um_AstigX=000um_AstigY=160um.png",
"DT120_blur7_Defoc=050um_AstigX=000um_AstigY=210um.png",
"DT120_blur7_Defoc=050um_AstigX=000um_AstigY=260um.png"}

I need to create pairs of files the paremeters of which are matching, i.e. to get

{{"DTideal_circ_def=000um_astX=000um_astY=410um.png","DT120_blur7_Defoc=000um_AstigX=000um_AstigY=410um.png"},
{"DTideal_circ_def=050um_defX=000um_defY=010um.png","DT120_blur7_Defoc=050um_AstigX=000um_AstigY=010um.png"}}

One Answer

First, join the two groups:

Join[files1, files2]

Then gather them by their last 3 numbers like:

StringCases["DT120_blur7_Defoc=050um_AstigX=000um_AstigY=260um.png", DigitCharacter ..]

(*Out: {"120", "7", "050", "000", "260"} *)


StringCases["DT120_blur7_Defoc=050um_AstigX=000um_AstigY=260um.png", DigitCharacter ..][[-3;;]]

(*Out: {"050", "000", "260"} *)


GatherBy[Join[files1, files2], StringCases[#, DigitCharacter ..][[-3 ;;]] &]

Then select groups that have two elements:

Select[GatherBy[Join[files1, files2], StringCases[#, DigitCharacter ..][[-3 ;;]] &], Length[#] == 2 &]

(*Out: {{"DTideal_circ_def=000um_astX=000um_astY=410um.png", 
         "DT120_blur7_Defoc=000um_AstigX=000um_AstigY=410um.png"}, 
        {"DTideal_circ_def=050um_defX=000um_defY=010um.png", 
         "DT120_blur7_Defoc=050um_AstigX=000um_AstigY=010um.png"}} *)

Correct answer by Ben Izd on June 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP