I have a list of strings in python consisting of various filenames, like this (but much longer):
all_templates = ['fitting_file_expdisk_cutout-IMG-HSC-I-18115-6,3-OBJ-NEP175857.9+655841.2.feedme', 'fitting_file_sersic_cutout-IMG-HSC-I-18115-3,3-OBJ-NEP180508.6+655617.3.feedme', 'fitting_file_sersic_cutout-IMG-HSC-I-18115-1,8-OBJ-NEP180840.8+665226.2.feedme', 'fitting_file_sersic_cutout-IMG-HSC-I-18115-6,7-OBJ-NEP175927.6+664230.2.feedme', 'fitting_file_expdisk_cutout-IMG-HSC-I-18114-0,5-OBJ-zsel56238.feedme', 'fitting_file_devauc_cutout-IMG-HSC-I-18114-0,3-OBJ-NEP175616.1+660601.5.feedme', 'fitting_file_sersic_cutout-IMG-HSC-I-18115-6,4-OBJ-zsel56238.feedme']
I'd like to create multiple smaller lists for elements that have the same object name (the substring starting with OBJ-
and ending right before .feedme
). So I'd have a list like this:
obj1 = ['fitting_file_expdisk_cutout-IMG-HSC-I-18114-0,5-OBJ-zsel56238.feedme', 'fitting_file_sersic_cutout-IMG-HSC-I-18115-6,4-OBJ-zsel56238.feedme']
,
and so on for other matching 'objects'. In reality I have over 900 unique 'objects', and the original list all_templates
has over 4000 elements because each object has 3 or more separate template files (which are all appearing in a random order to start). So in the end I'll want to have over 900 lists (one per object). How can I do this?
Edit: Here is what I tried, but it is giving me a list of ALL the original template filenames inside each sublist (which are each supposed to be unique for one object name).
import re
# Break up list into multiple lists according to substring (object name)
obj_list = [re.search(r'.*(OBJ.+)\.feedme', filename)[1] for filename in all_template_files]
obj_list = list(set(obj_list)) # create list of unique objects (remove duplicates)
templates_objs_sorted = [[]]*len(obj_list)
for i in range(len(obj_list)):
for template in all_template_files:
if obj_list[i] in template:
templates_objs_sorted[i].append(template)