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 ']
在上面的列表中,我有两个关键字(乳房和胸部)和相关的值。我需要选取每个关键字的最小值(按字数)。你知道吗
我想选1)'breast:entire breast '
2)'chest:entire chest wall '
你能帮忙吗?在Python中执行。你知道吗
希望这有帮助。你知道吗
您可以使用排序列表和dict来完成。 首先,您可以创建列表列表:
结果是:
现在我们可以根据第一个值和第二个值中单词的长度来排序
我们使用相反的方法将最小值放在排序列表的末尾,结果是:
现在从排序的列表中生成一个dict,dict有唯一的键,因此在处理结果时,将为每个第一个值取最后的值:
结果是
其他选择
您可以自定义或构建一个方法来获取所需的字符串格式,例如:
相关问题 更多 >
编程相关推荐