“Como encontrar a palavra mais curta em uma lista python” Respostas de código

Obtenha a palavra mais curta da lista Python

words = ["alpha","omega","up","down","over","under","purple","red","blue","green"]

#GET LONGEST WORD
max(words, key=len)
>>> 'purple'

#GET SHORTEST WORD
min(words, key=len)
>>> 'up'
Tirbo06

Como encontrar a palavra mais curta em uma lista python

def findShortest(lst):
    length = len(lst)
    short = len(lst[0])
    ret = 0
    for x in range(1, length):
        if len(lst[x]) < short:
            short = lst[x]
            ret = x

    return x # return the index of the shortest sentence in the list
Stupid Skylark

Respostas semelhantes a “Como encontrar a palavra mais curta em uma lista python”

Perguntas semelhantes a “Como encontrar a palavra mais curta em uma lista python”

Procure respostas de código populares por idioma

Procurar outros idiomas de código