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中执行。
答案 0 :(得分:1)
您可以使用排序列表和字典来做到这一点。 首先,您可以创建列表列表:
[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
)
我们使用反向将min值放在排序列表的末尾,结果是:
[['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 '}
答案 1 :(得分:0)
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 ']
string1 = 'breast:'
string2 = 'chest:'
c1 = float("inf")
c2 = float("inf")
for x in mylist:
if 'breast' in x :
c_idx = x.index(':')
x = x [ c_idx+1 : ]
cnt = x.count(" ")
if cnt < c1 :
string_b = x
c1 = cnt
else :
continue
elif 'chest' in x :
c_idx = x.index(':')
x = x [ c_idx+1 : ]
cnt = x.count(" ")
if cnt < c2 :
string_c = x
c2 = cnt
else :
continue
print(string1+string_b)
print(string2+string_c)
希望这会有所帮助。
答案 2 :(得分:0)
其他选项
sublisted = [ x.split(":") for x in set(mylist)]
breast = min([ item[1] for item in sublisted if item[0] == "breast" ], key=len)
chest = min([ item[1] for item in sublisted if item[0] == "chest" ], key=len)
print(breast) #=> entire breast
print(chest) #=> entire chest wall
sublisted = [ x.split(":") for x in set(mylist) ]
def find_min(lst, str):
found = min([ item[1] for item in sublisted if item[0] == str ], key=len)
return str + ': ' + found
keys = { x[0] for x in sublisted }
for k in keys:
print(find_min(sublisted, k))
# chest: entire chest wall
# breast: entire breast