How to sort a list of user-provided words using Python?

Asked

Viewed 96 times

1

I’m trying to create a program where the user informs the list of words to put in alphabetical order, only when I put the program to run, it worked normally, but the list he gave me was listing letter by letter and quoting commas and spaces as well, only I wanted him to list only the words ordered in list form. I’ll leave the script and the result image here:

print('Digite as palavras desejadas para colocar em ordem alfabética.')
resposta = list(input())
resposta.sort()
print('Aqui está sua lista de palavras em ordem alfabética:')
print(resposta)

Resultado do script

1 answer

2


The problem is that when you pass a string to the constructor list, all characters in the string will be converted to individual elements of the list.

You then need to pick up the string (with the function input), divide it by spaces to get each word and finally draw it.

Would look like this:

resposta = sorted(input().split(" "))

Here we are basically picking up the user’s response (with the function input), by dividing the response by spaces using the method split (operation that already returns a list) and, finally, drawing it with the function sorted.

In short:

print('Digite as palavras desejadas para colocar em ordem alfabética.')

resposta = sorted(input().split(" "))

print('Aqui está sua lista de palavras em ordem alfabética:')
print(resposta)

But the problem with the above approach is that it does not return only the words. If, for example, it passes as input "Hello, how’s your life?", we would receive:

['Olá,', 'a', 'como', 'sua', 'vai', 'vida?']

This shows that the classification is working correctly, although characters like the comma remain as part of a "word".

To resolve this, you can split the input provided by a regular expression. So:

import re

regex = re.compile("\W+", re.U)
resposta = sorted(filter(len, regex.split(input())))

print(resposta)

Thus, with the same previous entry, we would obtain:

['Olá', 'a', 'como', 'sua', 'vai', 'vida']

The difference is because we are using regular expression \W+ to divide the string provided by the user, so that the string will be divided by any character that is not alphanumeric. Was also used filter(len, <list>) to remove "empty strings" from output, what may occur in some cases.


And, just to keep talking - by default, sorted (evidently) differentiates letters in high or low box. If you want to ignore this behavior in ordering, you can pass the named argument key as str.casefold, which converts all letters to lowercase. Thus:

sorted(<list>, key=str.casefold)

It’s a lot, I know, but details are important. :-)

Reference

  • 1

    Thank you for responding :)

Browser other questions tagged

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