返回列表结果 none

Returning list results in none

所以我正在开发一个小程序,通过 GUI 从给定文件中删除重复项,以学习如何使用 Python 制作 GUI。

我写了一个方法,应该接受 string,将其转换为 list,从 list 中删除重复项,它确实做到了这一点。 当我想要 return 结果时出现问题,因为如果我 print() returned 值它只会导致 None 被打印。但是,如果我 print() 我想要 return 的值,它会在方法中打印出正确的列表。

class 看起来像这样:

#Class that removes the duplicates
class list_cleaner():
    def __init__(self):
        self.result_list = []

    def clean(self,input_string, seperator, target):
        #takes a string and a seperator, and splits the string at the seperator. 
        working_list = self.make_list_from_string(input_string,seperator)

        #identify duplicates, put them in the duplicate_list and remove them from working_list 
        duplicate_list = []
        for entry in working_list:
            instances = 0
            for x in working_list:
                if entry == x:
                    instances =  instances + 1
            if instances > 1:
                #save the found duplicate
                duplicate_list.append(entry)
                #remove the duplicate from working list
                working_list = list(filter((entry).__ne__, working_list))

        self.result_list = working_list + duplicate_list 
        print(self.result_list) #Prints the result list
        return self.result_list

主要功能如下所示(注意:duplicate_remover 是 list_cleaner 的门面):

if __name__ == "__main__":
    remover = duplicate_remover()
    x = remover.remove_duplicates("ABC,ABC,ABC,DBA;DBA;DBA,ahahahaha", ",")
    print(x) #Prints none. 

TL;DR:

我有一个方法 f returns list l 是 class C 的一个属性。

如果我 print() l 作为 f 的一部分,则正在打印 l 的值。

如果我 return l 并将其存储在 f 范围之外的变量中,然后 print() 这个变量它会打印 None

提前致谢!

编辑 1:

请求了 duplicate_remover 代码。 看起来像这样:

class duplicate_remover():
    def remove_duplicates(self,input_string,seperator):
        my_list_cleaner = list_cleaner()
        my_list_cleaner.clean( input_string = input_string, seperator = seperator)

remove_duplicates 忘记了 return my_list_cleaner.clean(...) 的 return 值,这导致默认值 None 被 returned。