【发布时间】: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