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
Thank you for responding :)
– arcanist011