python - AttributeError: 'builtin_function_or_method' 对象没有属性 'split'

标签 python list attributeerror

我突然发现了一些错误。我无法解决它。基本上,我在出现“和”和“或”的地方分割输入,并删除同一子列表中具有 x 的序列,而不是 x 的序列。我的代码如下所示

 import string

 class list(object):

      def __init__(self, input):
          self.input = input

      def update_list(self):
          new_list = [i.split("and") for i in input.split("or")]
          print new_list

      def reduced(self):
          re_list = [m for m in new_list if not any("not" + m in new_list)]
          print re_list

 def main():
     my_input = raw_input("        ")
     print my_input
     my_list = list(my_input)
     my_list.update_list()
     my_list.reduced()

 if __name__ == '__main__':
     main()

我遇到的错误:

Traceback (most recent call last):
line 39, in <module>
   main()
line 32, in main
   my_list.update_list()
line 18, in update_list
    new_list = [i.split("and") for i in input.split("or")]
AttributeError: 'builtin_function_or_method' object has no attribute 'split'

我的输入如下所示:

apple and berry or not apple and apple or banana and not papaya

期望的输出:

[['apple', 'berry'],['banana', 'not papaya']]

我也使用Python 2.x系列

当我在 update_list() 中将输入替换为 self.input 时,我纠正了上述问题 但我收到了新的错误说明

  re_list = [m for m in new_list if not any("not" + m in new_list)]
  NameError: global name 'new_list' is not defined

最佳答案

单词input是Python中的标准函数,您应该避免使用它。但是,在本例中,在 update_list 方法中,您指的不是 input,而是 self.input。由于您没有指定 self. ,因此它正在标准 python 中查找 input 函数并假设您是这个意思。因此你的错误是

'builtin_function_or_method' object has no attribute 'split'

因为内置函数input没有属性split

编辑----

好吧,我会给你更多,因为你似乎还在挣扎。如果您希望对象在调用之间保留值,则需要使用“self”。更新对象的属性。我对你想要reduced做什么有点模糊,所以我可能错了。

注意:为了我自己的测试理智,我对输入进行了硬编码,但您可以将 raw_input() 语句放回原处。

此外,请远离可能破坏 Python 内置函数的名称,例如“list”和“input”。

class MyList(object):
    ''' Also, classes are usually Capitalized. '''
    def __init__(self, data):
        self.raw_data = data

    def update_list(self):
        ''' I added an additional loop in order to strip away spaces. '''
        self.parsed_data = [[ j.strip() for j in i.split("and") ] for i in self.raw_data.split("or") ]
        return self.parsed_data

    def reduce_list(self):
        self.reduced_data = [m for m in self.parsed_data if m[0] != "not "+m[1] and m[1] != "not "+ m[0]]
        return self.reduced_data

def test_my_list():
    input_data = """apple and berry or not apple and apple or banana and not papaya"""
    print(input_data)
    my_list = MyList(input_data)
    print(my_list.update_list())
    print(my_list.reduce_list())

>>> test_my_list()
apple and berry or not apple and apple or banana and not papaya
[['apple', 'berry'], ['not apple', 'apple'], ['banana', 'not papaya']]
[['apple', 'berry'], ['banana', 'not papaya']]

关于python - AttributeError: 'builtin_function_or_method' 对象没有属性 'split',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442706/

相关文章:

python - Pandas/matplotlib 线图不显示 x 轴文本标签

python - 在 Python 2.7 中设置列​​表对象的属性

python - 使用 Python 从一系列列表中选择项目

javascript - jQuery .on ("click") 异常事件

pandas - 获取唯一列值时出现属性错误

python - AttributeError: 模块 'tensorflow' 没有属性 'python'

python - 使用 mmap 读取具有偏移量的二进制文件

Python\Numpy : Comparing arrays with NAN

Python的多重继承: Picking which super() to call

运行脚本时 Python 属性错误 : type object 'BaseCommand' has no attribute 'option_list'