Python 返回列表结果为“无”
因此,我正在开发一个小程序,通过GUI从给定文件中删除重复项,以学习如何使用Python制作GUI 我已经写了一个方法,它应该接受一个Python 返回列表结果为“无”,python,list,return,nonetype,Python,List,Return,Nonetype,因此,我正在开发一个小程序,通过GUI从给定文件中删除重复项,以学习如何使用Python制作GUI 我已经写了一个方法,它应该接受一个字符串,将它转换成一个列表,从列表中删除重复项,它实际上就是这样做的。 当我想返回结果时会出现问题,因为如果我print()返回值,只会导致打印None。但是如果我print()在方法中返回的值将打印出正确的列表 这个班看起来是这样的: #Class that removes the duplicates class list_cleaner(): def
字符串
,将它转换成一个列表
,从列表
中删除重复项,它实际上就是这样做的。
当我想返回
结果时会出现问题,因为如果我print()
返回值,只会导致打印None
。但是如果我print()
在方法中返回的值将打印出正确的列表
这个班看起来是这样的:
#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的外观):
TL;博士:
我有一个方法f
返回list
l
,它是类C
的一个属性
如果iprint()
l
作为f
的一部分,则打印l
的值
如果我返回l
并将其存储在f
范围之外的变量中,然后print()
此变量将打印None
提前谢谢
编辑1:
请求了replicate\u 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\u duplicates
忘记返回my\u list\u cleaner.clean(…)
的返回值,这导致返回默认值None
。看起来像是duplicate\u remover
是罪魁祸首。我现在将发布duplicate\u 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)