【问题标题】:AttributeError: 'builtin_function_or_method' object has no attribute 'split'AttributeError: 'builtin_function_or_method' 对象没有属性 'split'
【发布时间】:2016-01-31 06:36:48
【问题描述】:

我突然出现了一些错误。我无法解决它。基本上,我在出现“和”和“或”的任何地方分割我的输入,并删除同一子列表中具有 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() 中将 input 替换为 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

【问题讨论】:

  • 你的意思是self.input,而不是input。

标签: python list attributeerror


【解决方案1】:

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

'builtin_function_or_method' 对象没有属性'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']]

【讨论】:

  • 我纠正了这个问题并得到了一个新的错误@RobertB re_list = [m for m in new_list if not any("not" + m in new_list)] NameError: global name 'new_list' is not defined感谢您的解释
  • 您的意思是将“new_list”之类的内容分配为对象的属性吗?在这种情况下,您需要在它们前面加上“self.”。我不确定如何处理您的评论,因为我不知道您在什么范围内执行它。如果这是要添加到您的示例中的代码,请继续并将其放入问题中,以便我可以在上下文中看到它。此外,在您的问题中,“def main”和“if main”语句的缩进不正确。
  • 哦,是的,我只是在这里发布时缩进不当。
  • 我只想删除 x 和 not x 两个文字,并将该子列表减少为空列表并将它们从我的解决方案中删除。这样我的输出只是[['apple', 'berry'], ['banana', 'not papaya']] 而我只想使用从update_list() 获得的new_list 我尝试将“self”与new_list 一起使用,但它没有用
猜你喜欢
  • 2021-10-30
  • 2018-11-29
  • 2018-03-27
  • 2020-11-27
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多