如何按名称对数据集中的图像进行分组

我有2个主要组ED和ES的图像数据集。每个组还属于不同的患者(001-> 100),并且每个患者都有一组图像。

我要列出两个列表,一个用于ED组患者,另一个用于ES组患者。

'patient001_ED_z009.png', 'patient001_ED_z010.png', 'patient001_ES_z001.png', 'patient001_ES_z002.png' This is a sample of the dataset images names, the main pattern here is like this: patientXXX_EY_zZZZ.png.

XXX are three digits represent the patient number, Y refers to the two main groups D, S --> ED, ES, While last ZZZ are three digits represent the patient's slice number, which varies from patient to another.

    import os

    path = 'dataset'
    patients = os.listdir(path)
    file_counts = len(patients)
    print(patients)
    # at this point I didn't know how to split them