从列表中选取最小的项目(字符串)

2024-06-02 09:09:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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中执行。你知道吗


Tags: and列表关键字systemwallchestmylistentire
3条回答
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)

希望这有帮助。你知道吗

您可以使用排序列表和dict来完成。 首先,您可以创建列表列表:

[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,dict有唯一的键,因此在处理结果时,将为每个第一个值取最后的值:

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 '}

其他选择

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 

相关问题 更多 >