【问题标题】:Top 5 longest character words (with respect to length)前 5 个最长的字符词(相对于长度)
【发布时间】:2022-09-30 21:08:13
【问题描述】:
*** Top 5 longest character words (with respect to length)***

file = open(\"C:/Users/Dell/Desktop/Demo.TXT\", mode=\"r\", encoding=\"utf8\")
    file = file.read().split()
    length = 0
    Longest_word = \"\"
    for word in file:
        if len(word) > length:

            length = len(word)
            Longest_word = word
    
    print(Longest_word, length)

这段代码只给出一个最长的单词,但我需要 5 个最长的单词

    标签: python-3.x pandas list


    【解决方案1】:

    使用排序()

    排序(文件,键=len)

    然后抓取最后 5 个索引

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 1970-01-01
      • 2020-06-22
      • 2014-08-31
      • 2021-12-29
      • 1970-01-01
      • 2019-04-13
      • 2020-08-22
      • 2019-05-03
      相关资源
      最近更新 更多