我正在做一项任务,已经有了一个大的开始,但不知道如何继续,我正在寻找一些建议(不是答案)。使用以下类:class CounterList:
__n_comparisons__ = 0
def __init__(self, data=None):
if data is None:
self.data = []
else:
self.data = data
self.__n_accesses__ = 0
def __getitem__(self, i):
self.__n_accesses__ += 1
return self.data[i]
def __setitem__(self, i, item):
self.__n_accesses__ += 1
if type(item) != CounterNode:
raise ValueError("Only Counter objects can be placed in a CounterList")
else:
self.data[i] = item
def __delitem__(self, key):
self.__n_accesses__ += 1
del(self.data[key])
def __len__(self):
return len(self.data)
def __repr__(self):
return repr(self.data)
def __contains__(self, item):
raise TypeError("You can't use the 'in' keyword with a CounterList")
def __eq__(self, other):
self.__n_comparisons__ += 1
return self.data == other
def insert(self, index, item):
if type(item) != CounterNode:
raise ValueError("Only Counter objects can be added to a CounterList")
else:
self.data.insert(index, item)
def index(self, a=None):
raise TypeError("You can't do that with a CounterList")
def append(self, item):
if type(item) != CounterNode:
raise ValueError("Only Counter objects can be added to a CounterList")
else:
self.data.append(item)
def get_accesses(self):
return self.__n_accesses__
@classmethod
def get_comparisons(cls):
return cls.__n_comparisons__
@classmethod
def reset_comparisons(cls):
cls.__n_comparisons__ = 0
class MyString:
def __init__(self, i):
self.i = i
def __eq__(self, j):
if type(j) != MyString:
CounterList.__n_comparisons__ += 1
return self.i == j
def __le__(self, j):
if type(j) != MyString:
CounterList.__n_comparisons__ += 1
return self.i <= j
def __ne__(self, j):
if type(j) != MyString:
CounterList.__n_comparisons__ += 1
return self.i != j
def __lt__(self, j):
if type(j) != MyString:
CounterList.__n_comparisons__ += 1
return self.i < j
def __gt__(self, j):
if type(j) != MyString:
CounterList.__n_comparisons__ += 1
return self.i > j
def __ge__(self, j):
if type(j) != MyString:
CounterList.__n_comparisons__ += 1
return self.i >= j
def __repr__(self):
return repr(self.i)
def __getattr__(self, attr):
'''All other behaviours use self.i'''
return self.i.__getattr__(attr)
class CounterNode:
def __init__(self, word, count=1):
self.word = MyString(word)
self.count = count
def __repr__(self):
return str(self.word) + ": " + str(self.count)
我需要编写一个顺序搜索程序,其输出格式为:
^{pr2}$
如果列表中的每个单词都与新列表中的单词进行比较,如果该单词不存在,则使用计数器1将该单词添加到列表中,如果该单词在列表中,则程序只需将该单词计数加1。在
到目前为止,我掌握的代码是:from classes_1 import CounterNode, CounterList
def word_counter_seq(words_list):
my_list = CounterList()
for new_word in words_list:
i = 0
q = 1
if not my_list:
new_counter = CounterNode (new_word, 1)
my_list.append(new_counter)
elif new_word == my_list[i].word:
my_list[i].count +=1
elif len(my_list)>1:
if new_word == my_list[i].word:
my_list[i].count +=1
i+=1
elif new_word == my_list[q].word:
my_list[q].count +=1
q+=1
else:
new_counter = CounterNode (new_word, 1)
my_list.append(new_counter)
else:
new_counter = CounterNode (new_word, 1)
my_list.append(new_counter)
return my_list
但是,对于现在的代码,它只会正确返回原始列表中的前两个元素,并且任何后续的项都将以计数器1作为单独的项返回。例如:['hello': 3, 'world': 3, 'test': 1, 'test': 1]
而不是:['hello': 3, 'world': 3, 'test': 2]