Longest word in a sentence in the list - Python

Asked

Viewed 1,885 times

8

The code in Python need to find the largest word in the typed phrase that will be stored in the list, being displayed later.

The only thing I know about the resolution of this code is using the function max() and that my code runs but does not return exactly what I want.

Example:

Entrada:
['Eu acho que vi um gatinho']
Saída:
['gatinho']

My code is like this:

text = input().split()

maxstring = list(map(str, text))

print (max(maxstring))

Outgoing vi instead of kitten

3 answers

7


First, the line below is completely unnecessary:

maxstring = list(map(str, text))

When you do the split of a string you will already get a list of strings. Make a map to convert them into string again would be redundant.

Second, the function of max shall use the largest operator, >, to define who is the highest value. When you use this operator in strings the result will respect the alphabetical order. That is, the return you got is vi for it will be the last word when you alphabetize it.

You want the biggest word and it demands comparing the sizes of strings. The function max has a parameter called key where you can pass a searchable object and, when defined, the return of this object for each itemn of the list that will be used as a basis for comparison. As it is size we are talking about, just use the function len:

max(text, key=len)
  • Yeah, I forgot about len. In case I use the function min() it returns me the smallest element of text?

  • @Alexfeliciano If you use the same logic, yes.

  • Got it, thanks for your help.

4

max(text, key=len)

Where text is the list with the sliced string

2

To solve this question we can use the following logic:

  1. Capture any sentence and add it to a list;
  2. Scroll through this list and capture the size of each word;
  3. Insert the size of each word in another list;
  4. Scroll through the two lists simultaneously associating each large word or its name.

With this logic I implemented the following code:

frase = input('Digite a frase: ').split()
tam_palavras = list()
for palavra in frase:
    tam_palavras.append(len(palavra))

maior = max(tam_palavras)
print('Maior(es) palavra(s):')
for a, b in zip(frase, tam_palavras):
    if b == maior:
        print(a)

See here the operation of the programme.

Note that when we execute the code we receive the following message: Digite a frase: . Right now we should type in any sentence and press enter.

After we have inserted that sentence the code will execute the first time. It is through him that the list will be assembled tam_palavras, in which the size of all words that may be in frase. The tamanho of the largest word and stored in the variable maior.

Later the code executes the second for with the help of the function zip. It is through it that both lists - phrase and tam_words - will be simultaneously traversed. Moreover the block if which lies within the 2º for, will verify if the size "b" of each word is equal to greater. If yes, the word "a", listed frase will be displayed on the screen.

Observing

The use of second for is highly necessary. Because, if there is more than one larger word in the sentence, they will all be displayed.

Example 1

If the phrase typed is

Eu acho que vi um gatinho

The Exit will be...

Maior(es) palavra(s):
gatinho

In this case we have a phrase with only one larger word size, which is gatinho, and has 7 letters.

Example 2

If the phrase typed is:

Eu acho que vi um gatinho e uma gatinha

The exit will be...

Maior(es) palavra(s):
gatinho
gatinha

In this case we have a phrase with two larger words, which are; gatinho and gatinha. In this case each of the two words have 7 letters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.