java return none,返回列表结果为none

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

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

这个类看起来像这样:

#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

main函数看起来如此(注意: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 返回 list l 这是类 C 的属性 .

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

如果我返回 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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值