属性错误:'builtin_function_or_method'对象没有'split'属性。

3

我遇到了一些错误,无法解决。基本上,我想在出现“and”和“or”的地方拆分输入,并删除具有x、not 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 - khelwood
2个回答

2

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']]

我进行了更正,但出现了新的错误@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感谢解释。 - Belle
你是指将像"new_list"这样的东西分配为对象的属性吗?在这种情况下,你需要在它们前面加上"self."。关于你的评论,我不太确定该怎么处理,因为我不知道你在哪个范围内执行它。如果那是要添加到你的示例中的代码,请放在问题中,这样我就可以看到它的上下文了。另外,在你的问题中,"def main"和"if main"语句的缩进不正确。 - RobertB
哦,是的,我在这里发布时缩进不正确。 - Belle
我只想删除两个字面值,即 xnot x,并将子列表减少为空列表,并从我的解决方案中删除它们。这样,我的输出就是 [['apple', 'berry'], ['banana', 'not papaya']]。我只想使用从 update_list() 获得的 new_list。我尝试使用 "self" 和 new_list,但没有成功。 - Belle

0
应该使用 input().split() 而不是 input.split()。在 input 和 split 两个函数上都加上括号。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接