简体   繁体   English

查找最长的单词错误?

[英]Finding longest word error?

I'm trying to find the longest word in Python but I'm getting the wrong result. 我正在尝试在Python中找到最长的单词,但结果却是错误的。 The code below was done in interactive mode. 下面的代码是在交互模式下完成的。 I should've received 'merchandise' (10 characters) as the longest word/string but I got 'welcome' (7 characters) instead. 我应该以最长的单词/字符串收到“商品”(10个字符),但是却收到了“欢迎”(7个字符)。

 str = 'welcome to the merchandise store' #string of words
 longest = []                             #empty list
 longest = str.split()                    #put strings into list
 max(longest)                             #find the longest string
 'welcome'                                #Should've been 'merchandise?'

It's sorting the strings alphabetically not by length, what you need is: 它按字母顺序而不是长度对字符串进行排序,您需要的是:

max(longest, key=len)

Let me clarify a little bit further. 让我进一步澄清一下。 In Python the default comparison for strings is alphabetical. 在Python中,字符串的默认比较是按字母顺序进行的。 This means "aa" will be less than "abc" for all intents and purposes (cmp in python2 and < in python2/3). 这意味着“ aa”在所有意图和目的上都将小于“ abc”(在python2中为cmp,在python2 / 3中为<)。 If you call max on the list without a key then it will compare using the default comparison. 如果您在没有键的情况下调用列表中的max,则它将使用默认比较进行比较。 If you put in a key function then it will compare the key instead of the value. 如果您输入键功能,则它将比较键而不是值。 Another options (python2 only) is the cmp argument to max, which takes a function like cmp . 另一个选项(仅适用于python2)是max的cmp参数,它带有类似cmp的函数。 I don't suggest this method because it's going to end up being something like cmp=lambda x,y: len(x)-len(y) which seems much less readable then just key=len and isn't supported in python3. 我不建议使用此方法,因为它最终会变成类似cmp=lambda x,y: len(x)-len(y)似乎比key=len可读性差得多,并且在python3中不支持。

If you have any more questions about using key, I'd suggest reading this specifically note (7) which covers cmp and key for list.sort which uses them in the same manner. 如果您还有其他关于使用key的问题,建议阅读注释(7),其中涵盖了list.sort的cmp和key,它们以相同的方式使用它们。

You can also so do this: 您也可以这样做:

str = 'welcome to the merchandise store'
sorted(str.split(), key=len)[-1]

Split it, sort them by length, then take the longest (last one). 分割它,按长度排序,然后取最长的(最后一个)。

Change your str.split() to str.split(" ") 将您的str.split()更改为str.split(" ")

Then, 然后,

max = []
for x in str.split(" "):
    if len(x) > len(max[0]):
        max = []
        max.append(x)
    elif len(x) == len(max[0]):
        max.append(x)

This is one way to do it without the lambda or key=len just using a for loop and list comprehension : 这是没有lambdakey=len一种方法,仅使用for looplist comprehension

str = 'welcome to the merchandise store' 
longest = []                             
longest = str.split()
lst = []
for i in longest:
    x = len(i)
    lst.append(x)
y = [a for a in longest if max(lst)== len(a)]
print y[0]

Output: 输出:

merchandise

This is in Python 2, in Python 3 print (y[0]) 这是在Python 2中,在Python 3中显示 print (y[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM