Untitled

mail@pastecode.io avatar
unknown
plain_text
2 days ago
704 B
3
Indexable
Never
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}")
Leave a Comment