如何打印出最长的单词及其长度?

3
我正在尝试编写一个程序,它将读取一个单词文件,并打印出最长单词的长度和该长度的单词。
我已经发现在我的文件中有3个包括标点符号的13字母单词,但我需要程序自己找到最长的单词长度。
这是我的程序:
def main():
    filename = input("What is the filename?")
    with open(filename) as f:
        linenum = 1
        for line in f:
            words = line.split()
            longest = ''
            for word in words:
                if len(longest) < len(word):
                    longest = word
            print("Line", linenum, "has", longest, "as the longest word.")
            linenum += 1
            print(longest)
main()

我的程序返回:

What is the filename?test.txt
Line 1 has Working as the longest word.
Working
Line 2 has possibilities as the longest word.
possibilities
Line 3 has scrambled as the longest word.
scrambled
Line 4 has letters. as the longest word.
letters.
Line 5 has  as the longest word.

Line 6 has difficulties as the longest word.
difficulties
Line 7 has permutations. as the longest word.
permutations.
Line 8 has signature as the longest word.
signature
Line 9 has permutations. as the longest word.
permutations.
Line 10 has unscrambled as the longest word.
unscrambled

我需要输入一个字符函数吗?程序如何找到最长的13个字符单词。


你的文件很大吗?还是你要处理一个大文件? - The6thSense
1
你已经在寻找最长的单词了。只需获取该字符串的长度,然后重复搜索循环并提取具有该长度的单词。例如:for ...; max -> 13; for ... if (len(word) = max) - Marc B
4个回答

3

使用列表推导式:

lorem.py

Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a
type specimen book. It has survived not only five centuries, but
also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software
like Aldus PageMaker including versions of Lorem Ipsum.

long.py

def main():
    X=5
    print "Printing top "+str(X)+" longest words:"
    filename = "lorem.txt"
    # Can't get this under 80 characters:
    words = [(linenum, word) for linenum, line in enumerate(open(filename)) for word in line.split()]   
    words = sorted(words,key = lambda x: len(x[1]), reverse=True) #Sort by string length in reverse
    for word in words[:X]:
        print "The longest word '",word[1],"'is on line ",str(word[0])

if __name__ == "__main__":
    main()

输出

>>>
Printing top 5 longest words:
The longest word ' typesetting, 'is on line  5
The longest word ' popularised 'is on line  6
The longest word ' essentially 'is on line  5
The longest word ' typesetting 'is on line  0

你可以通过调整变量来检索前 X 个单词。
如果你不需要行号的话,这会更简单: long_no_linenum.py
def main():
    X=5
    print "Printing top " + str(X) + " longest words:"
    filename = "lorem.txt"
    words = (word for word in open(filename).read().split())
    words = sorted(words, key=len, reverse=True)   
    print [word for word in words[:X]]
if __name__ == "__main__":
    main()

无行号输出

>>> 
Printing top 5 longest words:
['typesetting,', 'typesetting', 'essentially', 'popularised', "industry's"]

1
def main():
real_longest = ['']
filename = input("What is the filename?")
with open(filename) as f:
    linenum = 1
    for line in f:
        words = line.split()
        longest = ''
        for word in words:
            if word[-1].isalpha() == False:
                word = word[:-1]
            if len(longest) < len(word):
                longest = word
        print("Line", linenum, "has", longest, "as the longest word.")
        if len(longest) > len(real_longest[0]):
            real_longest = [longest]
        elif len(longest) == len(real_longest[0]):
            real_longest.append(longest)
        linenum += 1
        print(longest)
for word in real_longest:
    print('This word is one of the largest:', word)

你难道不需要处理更多的字符吗?比如问号、感叹号、分号等等,而不仅仅是逗号和句号吗? - Ben Hare
我的代码现在打印出了“possibilities”,这很好,因为它是三个13个字符中的一个。我该如何让我的程序打印两次“permutations”? - Korrupted Studios
如果你需要获取三个单词,应该使用 real_longest = [] 而不是 real_longest = '',并且使用 real_longest.append(longest) 而不是 real_longest = longest。同时,将 len(longest) > len(real_longest) 改为 len(longest) >= len(real_longest)。最后(在脚本的结尾处),使用 for word in real_longest: print(word) - Kamejoin
太棒了,这给了我我想要的答案。最后一个问题,将 [0] 添加到语句 len(real_longest[0]) 中是什么意思? - Korrupted Studios
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/93409/discussion-between-kamejoin-and-korrupted-studios。 - Kamejoin
显示剩余4条评论

0
你可以使用 len(string) 来获取字符串的长度,然后你可以保留另一个变量来记录你已经看到过多少个该长度的单词,当你找到一个新的最长单词时,你需要重置这个变量。

0

在检查单词长度之前,您必须替换单词中的特殊字符。要从字符串中替换特殊字符,可以使用 [^A-Za-z] 字母过滤器。


以上解决方案仅用于计算确切的单词长度,如果要找到最长的单词,则必须使用Kamejoin的解决方案。 - Laxmikant Killekar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接