本文介绍了如何用python查找最长的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用python从一组单词中找到最长的单词?我可以找到第一个这样的单词:
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'
推荐答案
如果我正确理解了您的问题:
If I understand your question correctly:
>>> s = "a aa aaa aa"
>>> max(s.split(), key=len)
'aaa'
split()
将字符串拆分为单词(由空格分隔); max()
使用内置的len()
函数查找最大的元素,即字符串长度,以找出最大"的含义.
split()
splits the string into words (seperated by whitespace); max()
finds the largest element using the builtin len()
function, i.e. the string length, as the key to find out what "largest" means.
这篇关于如何用python查找最长的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!