Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建空列表并进行一些操作_Python_Regex_String - Fatal编程技术网

Python 创建空列表并进行一些操作

Python 创建空列表并进行一些操作,python,regex,string,Python,Regex,String,这就是我要做的。 我有一个列表,里面有字符串作为元素。现在我想用它做两件以上的事情 在类中创建空列表 对列表中的每个元素进行操作,然后将其添加到我创建的empyt列表中 我的代码到目前为止 class aer(object): def __init__(self): self.value = [] def rem_digit(self,s): self.value.append(re.sub(" \d+"," ",s)) def to_lower(self,s):

这就是我要做的。 我有一个列表,里面有字符串作为元素。现在我想用它做两件以上的事情

  • 在类中创建空列表
  • 对列表中的每个元素进行操作,然后将其添加到我创建的empyt列表中
  • 我的代码到目前为止

    class aer(object):
      def __init__(self):
        self.value = []
      def rem_digit(self,s):
        self.value.append(re.sub(" \d+"," ",s))
      def to_lower(self,s):
        self.value.append(self.s.lower())
    
    如果有人能指出我所犯的错误,那就太好了。 以及如何访问我在课堂上制作的“列表”

    样本列表:

    mki = ["@tenSjunkie We're sorry for the inconvenience. Please call the Guest Service Desk using this link http://t.co/8Zv8DFwbbu and your receipt.",
     "@lindz_h We're sorry for the inconvenience. Please call the Guest Service Desk using this link http://t.co/Ak9fnazHZN and your receipt."]
    
    比上一次有了一些进步,或者说我在班上被打败了

    def some_mani(old_list):
        new_list = []
        for i in range(0,len(old_list)):
            new_list.append(re.sub(" \d+"," ",old_list[i]).lower())
        return new_list
    

    我仍然想知道是否有人帮助我在类中构建它。

    我很难理解为什么你会想要这个,但听起来你想要一个包含字符串列表的类,以及一些执行简单字符串操作并将结果添加到列表中的方法。基于该假设,以下是一些代码:

    class MysteriousStringContainer(object):
        def __init__(self):
            self.values = []
    
        def remove_digits(self, s):
            self.values.append(re.sub("\d+", " ", s))
    
        def to_lower(self, s):
            self.values.append(s.lower())
    
    现在,您可以初始化神秘的StringContainer并开始调用其方法:

    >>> m = MysteriousStringContainer()
    >>> print m.values
    []
    >>> for s in mki:
    ...     m.remove_digits(s)
    ...
    >>> print m.values
    ["@tenSjunkie We're sorry for the inconvenience. Please call the Guest Service Desk using this link http://t.co/ Zv DFwbbu and your receipt.", "@lindz_h We're sorry for the inconvenience. Please call the Guest Service Desk using this link http://t.co/Ak fnazHZN and your receipt."]
    
    如您所见,数字已被删除,结果字符串可在
    m.values
    中找到。但是,在你继续使用它之前,我必须强调,无论你想做什么,这几乎肯定是一种可怕的方式。如果没有类的包装,上面的代码将写得更好:

    >>> nodigits = re.sub("\d+", " ", mki[0])
    >>> print nodigits
    "@tenSjunkie We're sorry for the inconvenience. Please call the Guest Service Desk using this link http://t.co/ Zv DFwbbu and your receipt."
    

    在这里,
    mki
    列表中仍然有原始字符串,没有数字的新字符串存储为
    nodigits
    变量。我想不出任何理由使用一个奇怪的、不直观的类设置来混淆先前存在的功能,但是如果这是您需要做的,我认为上面的代码可以做到。

    self.value.append(self.s.lower())
    应该是
    self.value.append(s.lower())
    您应该如何使用它?调用类方法时,您希望
    self.value
    包含什么?如果我错了,请纠正我。但我正在尝试做“操纵”,比如把“字符串”改成小写或删除数字。这些字符串是列表中的元素,因此我尝试获取列表,其中的元素与前面的字符串类似。@BhupendrasinhThakre是的。这(以及其他原因)就是为什么这不是进行字符串操作的好方法。为什么你不能简单地给你的结果赋值,而不进行奇怪的类和列表扭曲?我认为你建议我的方法更合适。我还将尝试通过python中可用的数据库类来实现这一点。