1

So the goal of this code is to remove duplicates from the input and then print out a list without the duplicates and I think I got it but I can't seem to remember how to take in input with spaces and none of the things I have looked up so far have been very helpful to my case. Here's my code.

def eliminateDuplicates(lst):
    strnumbers = str(lst)
    listnumbers = list(strnumbers.split())    
    newlist = []
    for number in listnumbers:
        if number not in newlist:
            newlist.append(number)
    return newlist

def main():
    numbers = int(input("Enter numbers separated by space"))
    print("The distinct numbers are: ", eliminateDuplicates(numbers)) 

main()
4

2 回答 2

1
strnumbers = str(lst)
listnumbers = list(strnumbers.split())  

I think this sillyness is the cause of your problems (, and [ characters are going into your numbers). Just iterate over the input lst.

You will also need to work at sending a proper list into your function, which means you will need to change this line:

numbers = int(input("Enter numbers separated by space"))

I will leave that bit up to you.

于 2013-11-01T03:15:50.760 回答
1

你不能做

int("1 2 3")

但你可以

[int(x) for x in "1 2 3".split()]

其中一个错误来自您的int(input())……但是我认为 wim 是对的

于 2013-11-01T03:14:44.360 回答