我遇到了一些错误,无法解决。基本上,我想在出现“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