Untitled
unknown
plain_text
a year ago
704 B
6
Indexable
def group_files_by_name_start(files): grouped_files = {} for file in files: name_start = '' for char in file: name_start += char if name_start not in grouped_files: grouped_files[name_start] = [] grouped_files[name_start].append(file) return grouped_files # Example usage: files = [ 'apple_pie.txt', 'apple_crisp.txt', 'banana_bread.txt', 'banana_muffin.txt', 'cherry_tart.txt', 'cherry_pie.txt' ] grouped = group_files_by_name_start(files) # Printing the grouped files for key, group in grouped.items(): if len(group) > 1: # Only print groups with more than one file print(f"Group '{key}': {group}")
Editor is loading...
Leave a Comment