简体   繁体   English

AttributeError: 'builtin_function_or_method' object 没有属性 'split'

[英]AttributeError: 'builtin_function_or_method' object has no attribute 'split'

I sprang up on some errors.我突然出现了一些错误。 I couldn't solve it.我无法解决它。 Basically I'm splitting my input wherever 'and' & 'or' occurs and deleting sequences having x, not x in the same sub-list.基本上,我在任何出现“和”和“或”的地方分割我的输入,并在同一个子列表中删除具有 x 而不是 x 的序列。 My code looks like this我的代码看起来像这样

 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()

errors I got:我得到的错误:

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'

my input looks like this:我的输入如下所示:

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

desired output:所需的 output:

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

also I use Python 2.x series我也使用 Python 2.x 系列

I rectified the above when I replaced input with self.input in the update_list() But I got new errors stating当我在 update_list() 中用 self.input 替换 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

The word input is a standard function in Python, you should avoid using it. input一词是Python中的标准功能,应避免使用它。 However, in this case, within the update_list method, you don't mean input you mean self.input . 但是,在这种情况下,在update_list方法中,您的意思并不是input而是self.input Since you didn't specify the self. 由于您未指定self. , it is finding the input function in standard python and assuming you mean that. ,它会在标准python中找到input函数,并假设您是这么做的。 Thus your error that a 因此,您的错误是

'builtin_function_or_method' object has no attribute 'split' 'builtin_function_or_method'对象没有属性'split'

since the builtin function input does not have a attribute split . 因为内置函数input没有属性split

EDIT---- 编辑 - -

Ok, I'll give you more since you seem to still be struggling. 好吧,我会给你更多,因为你似乎还在挣扎。 If you want an object to retain a value from call to call, you need to use "self." 如果希望对象在每次调用之间保留一个值,则需要使用“自我”。 to update an object's attributes. 更新对象的属性。 I'm a little vague on what you want reduced to do, so I may have that wrong. 对于您想reduced ,我有点含糊,所以我可能有错。

Note: for my own testing sanity I hard-coded the input, but you could put your raw_input() statement back in. 注意:出于我自己的测试理智,我对输入进行了硬编码,但是您可以放回raw_input()语句。

Also, stay away from names that could clobber Python built-ins, such as "list" and "input". 另外,请远离可能破坏Python内置功能的名称,例如“列表”和“输入”。

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

It should be input().split not input.split().它应该是 input().split 而不是 input.split()。 Add the brackets.添加括号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“ builtin_function_or_method”对象没有属性“ split” 3.7 - AttributeError: 'builtin_function_or_method' object has no attribute 'split' 3.7 AttributeError:&#39;builtin_function_or_method&#39;对象没有属性&#39;split&#39;2 - AttributeError: 'builtin_function_or_method' object has no attribute 'split' 2 AttributeError: &#39;builtin_function_or_method&#39; 对象没有属性 &#39;fieldnames&#39; - AttributeError: 'builtin_function_or_method' object has no attribute 'fieldnames' AttributeError:“ builtin_function_or_method”对象没有属性“ count” - AttributeError: 'builtin_function_or_method' object has no attribute 'count' AttributeError: &#39;builtin_function_or_method&#39; 对象没有属性 &#39;csv&#39; - AttributeError: 'builtin_function_or_method' object has no attribute 'csv' AttributeError:“ builtin_function_or_method”对象没有属性“ pop” - AttributeError: 'builtin_function_or_method' object has no attribute 'pop' AttributeError:“ builtin_function_or_method”对象没有属性“ addLayout” - AttributeError: 'builtin_function_or_method' object has no attribute 'addLayout' AttributeError:“ builtin_function_or_method”对象没有属性“ iterkeys” - AttributeError: 'builtin_function_or_method' object has no attribute 'iterkeys' AttributeError:'builtin_function_or_method'对象没有属性'replace' - AttributeError: 'builtin_function_or_method' object has no attribute 'replace' AttributeError:&#39;builtin_function_or_method&#39;对象没有属性&#39;connect&#39; - AttributeError: 'builtin_function_or_method' object has no attribute 'connect'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM