我无法弄清楚我做错了什么。我必须创建一个嵌套列表(对问题可能的解决方案),所以我创建了一个类,将解决方案附加到现有解决方案列表中(因为每个解决方案一次只能计算一个)在python中创建一个嵌套列表类
此函数可用精细:
def appendList(list):
#print list
list2.append(list)
x =0
while x < 10:
x = x+1
one = "one"
two = "two"
appendList([(one), (two)])
print list2
与结果:
[['one', 'two'], ['one', 'two'], ['one', 'two'], ['one', 'two'], ['one', 'two'], ['one', 'two'], ['one', 'two'], ['one', 'two'], ['one', 'two'], ['one', 'two']]
但是当我借此完全一样的功能,并把它放在一个班,我得到一个错误说:
TypeError: appendList() takes exactly 1 argument (2 given)
这里的班级,我怎么称呼它:
class SolutionsAppendList: list2 = [] def appendList(list): #print list list2.append(list)
appendList.appendList([('one'), ('two')])
我敢肯定,我正在做一个很基本的错误,但我似乎无法找出原因,因为我在课堂上使用了相同的功能。
任何建议/建议将是伟大的。 由于
'self'是不是一个法外定义,所以'self.list = []'不会在这里工作,这声明应该放在'__init__'中 –
你说得对,我不知道为什么我忽略了这个。 –
非常感谢。我喜欢你的解决方案。我将继续添加列表,并且当我想要访问它我只需键入'print appendList.list'是否有效?还是有更好的方法来访问我追加的列表? – Lostsoul