Skip to main content
 首页 » 编程设计

python之列表python中的多个最大值

2025年05月04日14lovecherry

嘿,作为 NEWBY,我想知道如何从列表中找到多个最大值(即不止一个最大值或相同长度的项目)和最小值(与最大值相同的位置)。我试过使用 max 函数,但它只打印一项,与 min 相同。这是针对列表中字符串的长度来完成的(例如使用len)!

这是我到目前为止的代码

    def choice4(filelist): 
       try: 
           c4longest=max(filelist,key=len) 
           print(c4longest,"is the longest item in the list") 

请您参考如下方法:

试试这个:

def choice4(filelist): 
    mymax = max(map(len,filelist)) 
    return [a for a in filelist if len(a)==mymax] 
 
a = ['joe','andy','mark','steve'] 
a.extend(a) 
print choice4(a)