【问题标题】:Class returns empty list类返回空列表
【发布时间】:2014-05-07 16:59:13
【问题描述】:

我正在尝试编写一个函数来清理 URL(将它们去除“www.”、“http://”等任何内容)以创建一个可以按字母顺序排序的列表。

我试图通过创建一个类来做到这一点,该类包括一个方法来检测我想从 URL 字符串中删除的术语,并将其删除。我苦苦挣扎的一点是,我想将修改后的 URL 添加到一个名为 new_strings 的新列表中,然后在我以不同的期限第二次调用该方法时使用该新列表,这样我就可以一步一步地可以从 URL 字符串中删除所有不需要的元素。

由于某种原因,我当前的代码返回一个空列表,我也很难理解是否应该将new_strings 传递给__init__?我想我对全局变量和局部变量有点困惑,非常感谢一些帮助和解释。 :)

谢谢!代码如下。

class URL_Cleaner(object):
    def __init__(self, old_strings, new_strings, term):
        self.old_strings = old_strings
        self.new_strings = new_strings
        self.term = term

    new_strings = []

    def delete_term(self, new_strings):
        for self.string in self.old_strings:
            if self.term in string:
                new_string = string.replace(term, "")
                self.new_strings.append(new_string)
            else:
                self.new_strings.append(string)
        return self.new_strings

    print "\n" .join(new_strings) #for checking; will be removed later

strings = ["www.google.com", "http://www.google.com", "https://www.google.com"]
new_strings = []
www = URL_Cleaner(strings, new_strings, "www.")

【问题讨论】:

  • Class __init__() 方法不返回任何内容,它们只是初始化第一个参数,通常命名为 self 的类实例。

标签: python list class


【解决方案1】:

我们为什么要创建一个类来做到这一点?

for string in strings:
     string.replace("www.","")

这不是你想要完成的吗?

不管问题出在你的类定义中。注意范围:

class URL_Cleaner(object):
    def __init__(self, old_strings, new_strings, term):
        """These are all instance objects"""
        self.old_strings = old_strings
        self.new_strings = new_strings
        self.term = term

    new_strings = [] # this is a class object

    def delete_term(self, new_strings):
        """You never actually call this function! It never does anything!"""
        for self.string in self.old_strings:
            if self.term in string:
                new_string = string.replace(term, "")
                self.new_strings.append(new_string)
            else:
                self.new_strings.append(string)
        return self.new_strings

    print "\n" .join(new_strings) #for checking; will be removed later
    # this is referring the class object, and will be evaluated when
    # the class is defined, NOT when the object is created!

我已在您的代码中注释了必要的原因.... 修复:

class URL_Cleaner(object):
    def __init__(self, old_strings):
        """Cleans URL of 'http://www.'"""
        self.old_strings = old_strings
        cleaned_strings = self.clean_strings()
    def clean_strings(self):
        """Clean the strings"""
        accumulator = []
        for string in self.old_strings:
            string = string.replace("http://", "").replace("www.", "")
            # this might be better as string = re.sub("http://(?:www.)?", "", string)
            # but I'm not going to introduce re yet.
            accumulator.append(string)
        return accumulator
        # this whole function is just:
        ## return [re.sub("http://(?:www.)?", "", string, flags=re.I) for string in self.old_strings]
        # but that's not as readable imo.

【讨论】:

  • 非常感谢您的回答和解决方案。一个简短的评论:我使用类的原因是因为我想从各种不同的项目中清除 URL,所以我想创建同一个类的不同实例。因此我也认为有必要将accumulator作为参数传递给clean_strings(),这样前一个实例创建的列表可以进一步修改?
  • 在进一步使用它之后,您的代码在第 5 行抛出错误:“未定义全局名称 'clean_strings'” - 有什么想法吗?
  • @Matthias 哎呀,应该是 self.clean_strings()
【解决方案2】:

你只需要将 new_strings 定义为

self.new_strings = []

并从构造函数中删除 new_strings 参数。

“new_strings”和“self.new_strings”是两个不同的列表。

【讨论】:

    猜你喜欢
    • 2019-08-18
    • 2021-03-11
    • 2015-10-27
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多