在 Python 中的列表中找到五个最长的单词并按字母顺序排列它们
Find five longest words in a List in Python and order them in alphabetical order
这是我第一次与 Python 一起完成 Uni 作业,我遇到了一个关于任务的困难:
提取整个文本中最长的五个单词,用这些项目创建一个列表并按字母顺序排列它们。在屏幕上打印出这些结果(例如,“本文中按字母顺序排列的五个最长的单词是:“word1”、“word2”、“word3”、“word4”、“word5”)
所以,到目前为止我创建的代码是:
longest_string = sorted(word_list, key=len)
print(longest_string)
second_longest_string = sorted(word_list, key=len)[-2]
print(second_longest_string)
third_longest_string = sorted(word_list, key=len)[-3]
print(third_longest_string)
fourth_longest_string = sorted(word_list, key=len)[-4]
print(fourth_longest_string)
fifth_longest_string = sorted(list_1, key=len)[-5]
print(fifth_longest_string) ```
我想我可以从这里开始,然后继续按字母顺序排列,但看起来这段代码每次都会生成不同的输出,因为在列表中有许多字符串具有相同的单词数。
知道我该如何继续吗?
这将根据长度对单词列表进行排序,然后得到 5 个最长的单词,然后根据字母顺序对它们进行排序
sorted_words = sorted(sorted(word_list, key=len)[-5:])
方法之一:
data = """
It's the first time I am working with Python for a Uni assignment and am facing a difficulty regarding a task that says: Extract five longest words in the entire text, create a List with those items and order them alphabetically. Print out these results on the screen"""
import re
# Remove all characters except words, space and ' from data
data = re.sub("[^a-zA-Z ']+", "", data)
words = sorted(data.split(), key=len)
print (f'Five longest words in this text ordered alphabetically are: {",".join(sorted(words[-5:]))}')
输出:
Five longest words in this text ordered alphabetically are: alphabetically,assignment,difficulty,regarding,results
import string
Text = """It's the first time I am, working with Python for a Uni assignment and I am facing a difficulty regarding a task that says: Extract five longest words in the entire text, create a List with those items and order them alphabetically."""
EditText = ''.join([x for x in Text if x in string.ascii_letters + '\'- ']) #get only words from string Text
Words = EditText.split(" ") # make a list with all worlds
SortedWords = sorted(sorted(Words, key=len)[-5:])
print(f'Five longest words in this text ordered alphabetically are: {", ".join(SortedWords)}.')
输出:
Five longest words in this text ordered alphabetically are: alphabetically, assignment, difficulty, longest, regarding.
这是我第一次与 Python 一起完成 Uni 作业,我遇到了一个关于任务的困难:
提取整个文本中最长的五个单词,用这些项目创建一个列表并按字母顺序排列它们。在屏幕上打印出这些结果(例如,“本文中按字母顺序排列的五个最长的单词是:“word1”、“word2”、“word3”、“word4”、“word5”)
所以,到目前为止我创建的代码是:
longest_string = sorted(word_list, key=len)
print(longest_string)
second_longest_string = sorted(word_list, key=len)[-2]
print(second_longest_string)
third_longest_string = sorted(word_list, key=len)[-3]
print(third_longest_string)
fourth_longest_string = sorted(word_list, key=len)[-4]
print(fourth_longest_string)
fifth_longest_string = sorted(list_1, key=len)[-5]
print(fifth_longest_string) ```
我想我可以从这里开始,然后继续按字母顺序排列,但看起来这段代码每次都会生成不同的输出,因为在列表中有许多字符串具有相同的单词数。
知道我该如何继续吗?
这将根据长度对单词列表进行排序,然后得到 5 个最长的单词,然后根据字母顺序对它们进行排序
sorted_words = sorted(sorted(word_list, key=len)[-5:])
方法之一:
data = """
It's the first time I am working with Python for a Uni assignment and am facing a difficulty regarding a task that says: Extract five longest words in the entire text, create a List with those items and order them alphabetically. Print out these results on the screen"""
import re
# Remove all characters except words, space and ' from data
data = re.sub("[^a-zA-Z ']+", "", data)
words = sorted(data.split(), key=len)
print (f'Five longest words in this text ordered alphabetically are: {",".join(sorted(words[-5:]))}')
输出:
Five longest words in this text ordered alphabetically are: alphabetically,assignment,difficulty,regarding,results
import string
Text = """It's the first time I am, working with Python for a Uni assignment and I am facing a difficulty regarding a task that says: Extract five longest words in the entire text, create a List with those items and order them alphabetically."""
EditText = ''.join([x for x in Text if x in string.ascii_letters + '\'- ']) #get only words from string Text
Words = EditText.split(" ") # make a list with all worlds
SortedWords = sorted(sorted(Words, key=len)[-5:])
print(f'Five longest words in this text ordered alphabetically are: {", ".join(SortedWords)}.')
输出:
Five longest words in this text ordered alphabetically are: alphabetically, assignment, difficulty, longest, regarding.