вы можете сделать это, используя отсортированный список и дикт.сначала вы можете создать список списков:
[x.split(':') for x in mylist]
результат:
[['breast', 'entire breast quadrant '],
['breast', 'entire breast '],
['breast', 'entire breast and endocrine system '],
['breast', 'entire breast quadrant '],
['breast', 'entire breast '],
['breast', 'entire breast and endocrine system '],
['chest', 'entire chest wall '],
['chest', 'entire chest wall artery '],
['chest', 'entire chest and abdomen and pelvis '],
['chest', 'entire chest wall '],
['chest', 'entire chest wall artery '],
['chest', 'entire chest and abdomen '],
['chest', 'entire chest and abdomen and pelvis '],
['chest', 'entire chest wall '],
['chest', 'entire chest wall artery '],
['chest', 'entire chest and abdomen '],
['chest', 'entire chest wall '],
['chest', 'entire chest wall artery ']
теперь мы можем упорядочить его по первому значению и длине слов во втором значении
sorted(
[x.split(':') for x in mylist],
key=lambda x: (x[0],len(x[1].split())),
reverse=True
)
мы используем обратное, чтобы положить минимальное значение в конец отсортированного списка, и в результате получим:
[['chest', 'entire chest and abdomen and pelvis '],
['chest', 'entire chest and abdomen and pelvis '],
['chest', 'entire chest wall artery '],
['chest', 'entire chest wall artery '],
['chest', 'entire chest and abdomen '],
['chest', 'entire chest wall artery '],
['chest', 'entire chest and abdomen '],
['chest', 'entire chest wall artery '],
['chest', 'entire chest wall '],
['chest', 'entire chest wall '],
['chest', 'entire chest wall '],
['chest', 'entire chest wall '],
['breast', 'entire breast and endocrine system '],
['breast', 'entire breast and endocrine system '],
['breast', 'entire breast quadrant '],
['breast', 'entire breast quadrant '],
['breast', 'entire breast '],
['breast', 'entire breast ']]
и теперь сделаем дикт из отсортированного списка, диктат имеетуникальные ключи, поэтому при обработке результата будут приниматься последние значения для каждого первого значения:
dict(sorted(
[x.split(':') for x in mylist],
key=lambda x: (x[0],len(x[1])),
reverse=True
))
результат равен
{'chest': 'entire chest wall ', 'breast': 'entire breast '}