Question 1 [3 marks]
Write a function that returns a new list by eliminating the duplicate values in the list. Use the following function header:
def eliminate_duplicates(my_list):
Write a main function that reads in numbers separated by a space in one line, changes the string to a list of integers, calls the function (eliminate_duplicates) and finally displays the result list returned by the function, as well as the unmodified original list of values. Here is the sample output:
Sample Output
Enter numbers: 2 3 77 3 2 1 7 1
The distinct numbers are: [2, 3, 77, 1, 7]
The original numbers are: [2, 3, 77, 3, 2, 1, 7, 1]
Another Sample Output:
Enter numbers: 44 76 44 34 98 34 1 44 99 1 1 1
The distinct numbers are: [44, 76, 34, 98, 1, 99]
The original numbers are: [44, 76, 44, 34, 98, 34, 1, 44, 99, 1, 1, 1]
NOTE:
The set class in Python represents objects that are unordered collection of unique elements. The order of the output must match the sample output, and so you cannot use the set class for this question.
You may assume that the user always enters a list of integer numbers separated by spaces. Input validation is not required.
The template for the program is as follows:
def eliminate_duplicates(my_list):
# Write the function code here
def main():
# Write the main function
# Call the main function
main()
tim@crunchbangtime:~/workspace/baidu_zhidao$ python3 qa.py
Enter numbers: 2 3 77 3 2 1 7 1
The distinct numbers are: [2, 3, 77, 1, 7]
The original numbers are: [2, 3, 77, 3, 2, 1, 7, 1]
Enter numbers: 44 76 44 34 98 34 1 44 99 1 1 1
The distinct numbers are: [44, 76, 34, 98, 1, 99]
The original numbers are: [44, 76, 44, 34, 98, 34, 1, 44, 99, 1, 1, 1]
Enter numbers:
tim@crunchbangtime:~/workspace/baidu_zhidao$
大致是这样。你试试吧,我没有校验。
额,这是我们的练习题,然后我不会,所以说如果最好的话可以告诉我你的微信或者是QQ吗?