我应该怎么做才能按用户在“名称”中设置函数中的变量__主要的;用python?

我应该怎么做才能按用户在“名称”中设置函数中的变量__主要的;用python?,python,python-3.x,Python,Python 3.x,我想让用户告诉哪些列表要删除重复的,我该如何处理这个 my_list = [1,2,3,4,3,5] cheak = [] def duplicate(any_list): for things in any_list: if things not in cheak: cheak.append(things) else: pass return cheak if __name__ == "__ma

我想让用户告诉哪些列表要删除重复的,我该如何处理这个

my_list = [1,2,3,4,3,5]
cheak = []
def duplicate(any_list):
    for things in any_list:
        if things not in cheak:
            cheak.append(things)
        else:
             pass
    return cheak

if __name__ == "__main__":
    duplicate(my_list)
您必须先分配我的_列表,然后再将其传递给副本。在循环之外。

IIUC您需要:

if __name__ == "__main__": 
    my_list = [1,2,3,4,3,5]
    duplicate(my_list)
def duplicate(any_list):
    cheak = []
    for things in any_list:
        if things not in cheak:
            cheak.append(things)
        else:
             pass
    return cheak

if __name__ == "__main__":
    my_list = [1,2,3,4,3,5]
    duplicate(my_list)

以下代码是您需要的:

if __name__ == "__main__": 
    my_list = [1,2,3,4,3,5]
    duplicate(my_list)
def duplicate(any_list):
    cheak = []
    for things in any_list:
        if things not in cheak:
            cheak.append(things)
        else:
             pass
    return cheak

if __name__ == "__main__":
    my_list = [1,2,3,4,3,5]
    duplicate(my_list)
输出:

def duplicate(any_list):
    '''remove duplicates from list'''

    cheak = []
    for things in any_list:
        if things not in cheak:
            cheak.append(things)
    return cheak

def mainFun():
    my_list = [1,2,3,4,3,5]
    print(duplicate(my_list))

if __name__ == "__main__":
    mainFun()
还要注意的是,
else
条件不是必需的

更快的实现,更符合您的愿望:

[1, 2, 3, 4, 5]

我已经写了两种类型的解决方案

  • duplicate
    函数使用局部变量(
    func_loc_cheak
    )(在函数范围内)并返回此局部列表的内容

  • duplicate_glob
    函数使用一个全局变量(
    cheak_global
    ),它不会返回任何内容,因为您可以在不指定变量的情况下使用此全局变量

  • 我添加了一些打印以查看函数之间的行为和差异

    代码:

    def duplicate(any_alist):
        '''remove duplicates from list'''
    
        return list(set(any_alist))
    
    cheak_global = []
    
    
    def duplicate_glob(any_list):
        for things in any_list:
            if things not in cheak_global:
                cheak_global.append(things)
    
    
    def duplicate(any_list):
        func_loc_cheak = []
        for things in any_list:
            if things not in func_loc_cheak:
                func_loc_cheak.append(things)
        return func_loc_cheak
    
    
    if __name__ == "__main__":
        # Test lists.
        my_list = [1, 2, 3, 4, 3, 5]
        my_other_list = [8, 9, 0]
    
        result = duplicate(my_list)
        print("After first function call: {}".format(result))
    
        result = duplicate(my_other_list)
        print("After second function call: {}".format(result))
    
    
        print("Content of 'cheak_global' before function call: {}".format(cheak_global))
    
        duplicate_glob(my_list)
        print("Content of 'cheak_global' after first function call: {}".format(cheak_global))
    
        duplicate_glob(my_other_list)
        print("Content of 'cheak_global' after second function call: {}".format(cheak_global))
    
    输出:

    def duplicate(any_alist):
        '''remove duplicates from list'''
    
        return list(set(any_alist))
    
    cheak_global = []
    
    
    def duplicate_glob(any_list):
        for things in any_list:
            if things not in cheak_global:
                cheak_global.append(things)
    
    
    def duplicate(any_list):
        func_loc_cheak = []
        for things in any_list:
            if things not in func_loc_cheak:
                func_loc_cheak.append(things)
        return func_loc_cheak
    
    
    if __name__ == "__main__":
        # Test lists.
        my_list = [1, 2, 3, 4, 3, 5]
        my_other_list = [8, 9, 0]
    
        result = duplicate(my_list)
        print("After first function call: {}".format(result))
    
        result = duplicate(my_other_list)
        print("After second function call: {}".format(result))
    
    
        print("Content of 'cheak_global' before function call: {}".format(cheak_global))
    
        duplicate_glob(my_list)
        print("Content of 'cheak_global' after first function call: {}".format(cheak_global))
    
        duplicate_glob(my_other_list)
        print("Content of 'cheak_global' after second function call: {}".format(cheak_global))
    

    你知道如何在ˋuˋuˋuˋmainˋuˋguard之外做这件事吗?你能添加例外的输入和输出,这样我们就能给你更好的解决方案。你可以在一行中完成这件事,
    cheak=list(set(myˋu list))
    它会删除所有重复的内容。