鱼C论坛

 找回密码
 立即注册
查看: 1376|回复: 2

[已解决]python47讲课后作业

[复制链接]
发表于 2020-10-18 13:28:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以下为我写的代码,来实现记录对于目标的查找次数,并增加了添加功能和删除功能。但是调试中发现,在self,count这个字典中,新加的项的排序是有问题的,所以想问各位,如果解决这个问题?谢谢。
class CountList:
    def __init__(self, *args):
        self.values = [x for x in args]
        self.count = dict()
        self.count = dict.fromkeys(range(len(self.values)) , 0)
                 
    def __len__(self):
        return len(self.values)

    def __getitem__(self, index):
        self.count[index] += 1
        return self.values[index]
   
    def __setitem__(self,index,value):
        self.values.insert(index , value)
        for i  in range (index,len(self.values)-1):
            self.count[ i + 1] = self.count[ i ]
        self.count [index] = 0
   
        self.values[index] = value
        

    def __delitem__(self,index):
        self.values.remove(index)
        del self.count[index]
        for i in range (index, len(self.values)-1):
            self.count[ i ] = self.count[ i +1]
    def counter(self, index):
        return self.count[key]
最佳答案
2020-10-18 13:34:42
使用OrderedDict代替dict
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-18 13:34:42 | 显示全部楼层    本楼为最佳答案   
使用OrderedDict代替dict
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-18 13:51:30 | 显示全部楼层
kogawananari 发表于 2020-10-18 13:34
使用OrderedDict代替dict

谢谢指导!完美解决。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 18:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表