+ 2

Python 62

Задание: Используя текст в качестве вводных данных, найдите и выведите в результат самое длинное слово. Пример вводных данных this is an awesome text Пример результата awesome Я написала: txt = input() words = txt.split(" ") w = max(len(i) for i in words) for k in words: if len(k)==w: print(k) Помогите пожалуйста, как здесь можно было бы написать чище

10th Nov 2021, 3:58 PM
Дарья Москалева
Дарья Москалева - avatar
3 odpowiedzi
+ 7
Дарья Москалева , it can be also done with a list of separated words, then using the max() function: lst = input().split() # input text will be split at word boundaries = spaces print(max(lst, key= len)) # it uses max() function with a key for length of the items
10th Nov 2021, 6:17 PM
Lothar
Lothar - avatar
0
Your solution is good already. Here is a way to use fewer lines, but it is harder to read: #build dictionary of lengths with words words = {len(word):word for word in input().split()} print(words[max(words.keys())]) Edit: If there is more than one word equal to the maximum length, then this approach shows only the last one.
10th Nov 2021, 5:51 PM
Brian
Brian - avatar
0
txt = input() print(max(txt.split(' '), key=len))
24th Nov 2021, 7:02 PM
Callie Schwieder