python的问题,是python3以上的,求答案

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$

#!/usr/bin/env/python3.2

def eliminate_duplicates(my_list):
    # Write the function code here
    def _iterfilterexceptexists(my_list):
        exists = set()
        for element in my_list:
            if element not in exists:
                exists.add(element)
                yield element
    return list(_iterfilterexceptexists(my_list))


def main():
    while 1:
        datastr = input("Enter numbers: ")
        if datastr:
            original = list(map(int, datastr.split(' ')))
            result = eliminate_duplicates(original)
            print("The distinct numbers are: %r\nThe original numbers are: %r\n" %
                (result, original))
        else:
            break

if __name__ == "__main__":
    main()
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-29
def eliminate_duplicates(my_list):
    return list(set(my_list))
    
def main():
    num_in = input("Enter number:")
    original = [int(x) for x in num_in.split()]
    print('The original numbers are: %s'%original)
    distinct = eliminate_duplicates(original)
    print('The distinct numbers are: %s'%distinct)   
    
main()


大致是这样。你试试吧,我没有校验。

第2个回答  2014-10-29
def eliminate_duplicates(my_list):
    # Write the function code here
    ret_list = []
    for ml in my_list:
     if ml not in ret_list:    # 使用了一个not in ,新list里头没这个数字就塞进去
     ret_list.append(ml)
    # 上面三行代码或者写成这样[ret_list.append(ml) for ml in my_list if ml not in ret_list]
    return ret_list

def main():
    # Write the main function
    my_list = raw_input('Enter numbers:').split(' ')   # 把输入的一串数字,按空格转为list
    print ('The original numbers are: ', eliminate_duplicates(my_list))
    print ('The distinct numbers are: ', my_list)

# Call the main function
main()

第3个回答  2014-10-29
你好:
你至少应该描述下!追问

额,这是我们的练习题,然后我不会,所以说如果最好的话可以告诉我你的微信或者是QQ吗?

相似回答