Length of longest word in a list

后端 未结 6 1242
面向向阳花
面向向阳花 2020-12-06 19:10

What is the more pythonic way of getting the length of the longest word:

len(max(words, key=len))

Or:

max(len(w) for w in words)

6条回答
  •  清歌不尽
    2020-12-06 19:36

    I know it's been a year now but neverthless, I came up with this:

    '''Write a function find_longest_word() that takes a list of words and returns the length of the longest one.'''

    a = ['mamao', 'abacate', 'pera', 'goiaba', 'uva', 'abacaxi', 'laranja', 'maca']
    
    def find_longest_word(a):
    
        d = []
        for c in a:
            d.append(len(c))
            e = max(d)  #Try "min" :D
        for b in a:
            if len(b) == e:
                print "Length is %i for %s" %(len(b), b)
    

提交回复
热议问题