Reading the comments understood what is happening, when typing the list you use the literal python list syntax itself to insert it as input and the answers use a list syntax whose items are separated by space and the input is not bounded by brackets.
So I’m not gonna hold down the function remove_repetidos()
because the other answers have already explained the subject and then I will define it in a way that visually goes unnoticed:
remove_repetidos = lambda l: list(sorted(set(l)))
I’ll focus more on the console input treatment.
Let’s take three entrees:
[3,2,5,2,4,0,5,2,3]
3,2,5,2,4,0,5,2,3
3 2 5 2 4 0 5 2 3
From just looking we can conclude:
All these entries can be understood as being a list typed by the user.
They may or may not be started and finished by square brackets []
.
The content that matters is its numerical elements and not the separators.
Aware of this we can treat the user input by removing all that is not numerical and taking advantage of these non-numerical characters to group the digits and thus separate the numbers to recomporm them a list of integers.
To remove what is not numerical and separate the input into numerical strings I used a Regular Expression and method re.split()
that divides a string by the occurrences of a pattern.
The standard used was (?a)\D+
where:
- (?to) equals the constant re.ASCII what force to pattern \D the ASCII capture.
- \D+ means capturing one or more of everything that is not an ASCII digit.
To remove the empty captures that can be generated by duplicate separators or separators present at the beginning and end of the input I used the built-in function filter()
objectionable None
as a filter parameter.
To convert numeric strings into integers I used the built-in function map
to apply the constructor of the class int
each element previously returned by filter()
.
import re
numeros = input("Digite uma lista com números inteiros: ")
#Cria uma lista de inteiros apartando as strings numérica de
#tudo aquilo que não for número decimal
lista = list(map(int, filter(None, re.split('(?a)\D+', numeros ))))
print("Você digitou os seguintes números: ")
print(lista)
#Cria a função remove_repetidos que cria e ordena um set a #partir de uma lista
remove_repetidos = lambda l: list(sorted(set(l)))
print("Sua lista filtrada e ordenada: ")
print(remove_repetidos(lista))
Test the code in the Repl.it: https://repl.it/repls/WickedMintcreamDaemon
Output example:
Digite uma lista com números inteiros: [1,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,44,4,4,4,4,4]
Você digitou os seguintes números:
[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 44, 4, 4, 4, 4, 4]
Sua lista filtrada e ordenada:
[1, 2, 4, 44]
Output example:
Digite uma lista com números inteiros: 7 6 7 6 5 8 9
Você digitou os seguintes números:
[7, 6, 7, 6, 5, 8, 9]
Sua lista filtrada e ordenada:
[5, 6, 7, 8, 9]
What is your doubt, I did a test and its function removed the duplicates and ordered... It is not actually returning, but part of the code is working.
– Daniel Mendes
Hello I type a list for example
[1,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,44,4,4,4,4,4]
and he returns me the same list instead of returning[1,2,4,44]
– RafaelSacramentoo