python中OBJECT的问题,求大家帮帮忙!
时间:2011-08-17
来源:互联网
Python code
求大家帮忙看看REMOVE这一部分,我真不知道怎么写了。
昨天谢谢大家的帮忙。。
#!/usr/bin/env python """ A simple program that plays Shannon's Game. """ class Frequency(object): """ Stores a letter:frequency pair. >>> f = Frequency('c', 2) >>> f.letter 'c' >>> f.frequency 2 >>> f {c: 2} """ def __init__(self, letter, frequency): self.letter = letter self.frequency = frequency # The next Frequency object when stored as part of a linked list self.next = None def __repr__(self): return '{%s: %d}' % (self.letter, self.frequency) class FrequencyList(object): """Stores a collection of Frequency objects as a linked list.""" def __init__(self): self.head = None def add(self, letter, frequency): """ Adds the given `letter`:`frequency` combination as a Frequency object to the end of the list. If the given `letter` is already in the list, the given `frequency` is added to its frequency. >>> f = FrequencyList() >>> f.add('a', 3) >>> f [{a: 3}] >>> f.add('b', 2) >>> f [{a: 3}, {b: 2}] """ self.letter = letter self.frequency = frequency if self.head is None: self.head = Frequency(letter, frequency) else: current = self.head while current.next is not None and current.letter != letter: current = current.next if current.letter != letter: current.next = Frequency(letter, frequency) return def remove(self, letter): """ Removes the Frequency object with the given `letter` from the list. Does nothing if `letter` is not in the list. >>> f = FrequencyList() >>> f.add('a', 3) >>> f.add('b', 2) >>> f.add('c', 3) >>> f [{a: 3}, {b: 2}, {c: 3}] >>> f.remove('b') >>> f [{a: 3}, {c: 3}] """ pass
求大家帮忙看看REMOVE这一部分,我真不知道怎么写了。
昨天谢谢大家的帮忙。。
作者: gozhuzhu 发布时间: 2011-08-17
直接用python自带的list。
Python又不是C,从来没人自己实现链表。
Python又不是C,从来没人自己实现链表。
作者: iambic 发布时间: 2011-08-17
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28