How can I use python to find the longest word from a set of words? I can find the first word like this:
'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)]
'a'
rfind is another subset
'a aa aaa aa'[:'a aa aaa aa'.rfind(' ',1,10)]
'a aa aaa'
How can I use python to find the longest word from a set of words? I can find the first word like this:
'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)]
'a'
rfind is another subset
'a aa aaa aa'[:'a aa aaa aa'.rfind(' ',1,10)]
'a aa aaa'
Here is one from the category "How difficult can you make it", also violating the requirement that there should be no own class involved:
The interesting bit is that you use an object member to maintain state while the list is being computed in the comprehension, eventually discarding the list and only using the side-effect.
But please do use one of the max() solutions above :-) .
If I understand your question correctly:
split()
splits the string into words (seperated by whitespace);max()
finds the largest element using the builtinlen()
function, i.e. the string length, as the key to find out what "largest" means.Another way to find longest word in string: