-1

I have a problem as I am a total beginner to Python, now learning text-manipulation. I am trying to extract the five longest words in a text, create a list with those items and order them alphabetically. Then I need to print the results.
How do I proceed?

clarkens
  • 1
  • 1

2 Answers2

0
strings = input("Enter a Text: ").split() # split the text into a list of words
sorted_list = list(sorted(strings, key = len,reverse=True))[:5] # sort the list by length of the words
ans = sorted(sorted_list) # sort the list alphabetically
print(ans)  # print the list
Dharman
  • 30,962
  • 25
  • 85
  • 135
king juno
  • 11
  • 1
  • 4
-1

Thank you for the input. I tried to do so but still I am doing something wrong. Now I started to create a list instead to convert it to a set, as set does not allow duplicates. Then I convert it back to a list to sort it based on how many characters the text have. The last thing I need to do is to pick the 5 first item of the list. Right now it looks like this, and it does not work. What am I missing?

#creating a list

myTopFiveList = []

#converting the list to a set, as it does not allow duplicates

my_set = set(myTopFiveList)

#converting it back to a list

myTopFiveList = list(my_set)

my_set.add(text)

print(list(my_set))

#sort the new list based on the number of characters

myTopFiveList.sort(key=lambda x: len(x.split(" ")), reverse=True)

#sort the new list based on the number of characters

total = 0 for text in text: total += len(text)

print (total)

#pick the first 5 items of the list

clarkens
  • 1
  • 1