python调⽤其他class_python中⼀个class调⽤另⼀个class的
print问题?
python新⼿,不是很清楚类的调度。我已经构造了⼀个limited stack的类。
class LimitStack():
def __init__(self,size,initValue):
self.size = size
self.stack = [initValue]*size
self.head = size -1
def __str__(self):
return str(self.stack)
def push(self,ele):
self.head = 0 if (self.head + 1 >= self.size) else self.head + 1
self.stack[self.head] = ele
def top(self):
return self.stack[self.head]
def get(self,idx):
python新手怎么兼职
#Get the element of certain index (index 0 starts from the last pushed element)
if (idx >= 0 and idx < self.size):
pos = (self.size+self.head-idx) if (self.head-idx < 0) else (self.head - idx)
return self.stack[pos]
然后我⼜构建了⼀个新的class DPcache,是⼀个字典结构,其中需要调⽤limitstack。
class DPcache():
def __init__(self,dictionary):
self.dict = {}
self.dictionary = dictionary
def __str__(self):
return str(self.dict)
#Push the mth cuttings
def push(self,cuttings,taul):
#add words to cache if they first appear in our view
for cutting in cuttings:
if cutting[0] not in self.dict.keys():
self.dict[cutting[0]] = LimitStack(taul,0)
希望输出的结构就是类似
{'得': [0,0,0,0],'⼼得':[0,0,0,0]}堆栈是为了之后做迭代⽤的。现在问题是print的时候输出{'得': }的格式。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。