How to accept a list as input?

0

This is an exercise in a USP course. I will post the statement and the code so that the doubt is clear.

Write the remove_repetitions function that receives a list of integers as a parameter, checks to see if it has repeating elements and removes them. The function should return a list corresponding to the first list, without repeated elements. The returned list must be sorted.

My code:

def remove_repetidos(x):
    c = 0

    x.sort()

    while c < len(x)-1:
        if x[c] == x[c+1]:
            del x[c]
        else:
            c = c+1
    return x


x = input("Digite sua lista:")

lista = remove_repetidos(x)

print (lista)

I know the code is not optimized. But the question is, how to adapt the function so that it receives several lists as input?

For example, does the user want to rotate the code and type any list and return a list with the repeated numbers removed?

Abs,

    
asked by anonymous 07.08.2017 / 21:04

1 answer

2

When you take data via input( ) Python presumes to be a string . To convert to "list", in fact array , enclose the function input( ) with eval( ) , already placed the brackets, and enter the values separated by commas:

x = eval('[' + input("Digite sua lista: ") + ']')

Example:

>>> Digite sua lista: 4, 4, 8, 6, 7, 9, 9
>>> [4, 6, 7, 8, 9]

At the same time, you will have the margin of all possible errors if you enter an undue value, such as string in one of the indexes, for example [3, 4, 'a', 3] , because of the sort( ) function.

    
08.08.2017 / 16:09