鱼C论坛

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

[已解决]课后题求助 :第047讲:魔法方法:定制序列

[复制链接]
发表于 2021-10-1 23:38:56 | 显示全部楼层 |阅读模式

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

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

x
第47讲:魔法方法:定制序列 第0题求助:

该题目是要实现一个可修改的能够记录元素访问次数的序列

我的代码中用字典来记录元素访问次数{key = 序列中的元素值 : value = 访问次数}

现在有一个问题: del 一个元素的时候, 只能删除掉序列中的元素值,但是删除不掉counts字典里的键值对,如下图:
微信图片_20211001233747.png

请问各位大神帮忙看一下 有哪些问题




代码如下:
  1. class CountList(list):
  2.     def __init__(self, *args):
  3.         super().__init__(args)
  4.         self.counts = {}.fromkeys(self, 0)

  5.     def __len__(self):
  6.         return len(self.counts)

  7.     def __getitem__(self, item):
  8.         temp = super().__getitem__(item)
  9.         self.counts[temp] += 1
  10.         return temp

  11.     def __setitem__(self, key, value):
  12.         if key in self.counts:
  13.             self.counts[key] += 1
  14.         else:
  15.             self.append(value)
  16.         super().__setitem__(key, value)

  17.     def __delete__(self, instance):
  18.         self.counts.pop(super().__getitem__(instance))
  19.         super().__delitem__(instance)

  20.     def append(self, value):
  21.         self.counts[value] = 0
  22.         super().append(value)
复制代码
最佳答案
2021-10-2 09:35:41
  1. class CountList(list):
  2.     def __init__(self, *args):
  3.         super().__init__(args)
  4.         self.counts = {}.fromkeys(self, 0)

  5.     def __len__(self):
  6.         return len(self.counts)

  7.     def __getitem__(self, item):
  8.         temp = super().__getitem__(item)
  9.         self.counts[temp] += 1
  10.         return temp

  11.     def __setitem__(self, key, value):
  12.         if key in self.counts:
  13.             self.counts[key] += 1
  14.         else:
  15.             self.append(value)
  16.         super().__setitem__(key, value)

  17.     def __delitem__(self, instance): # 你的问题出现在这里(用 __delitem__)
  18.         self.counts.pop(super().__getitem__(instance))
  19.         super().__delitem__(instance)

  20.     def append(self, value):
  21.         self.counts[value] = 0
  22.         super().append(value)
  23.    
  24. c = CountList(1, 2, 3, 4, 5)

  25. del c[3]

  26. print(c.counts)

  27. print(c)
复制代码
  1. {1: 0, 2: 0, 3: 0, 5: 0}
  2. [1, 2, 3, 5]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-2 09:35:41 | 显示全部楼层    本楼为最佳答案   
  1. class CountList(list):
  2.     def __init__(self, *args):
  3.         super().__init__(args)
  4.         self.counts = {}.fromkeys(self, 0)

  5.     def __len__(self):
  6.         return len(self.counts)

  7.     def __getitem__(self, item):
  8.         temp = super().__getitem__(item)
  9.         self.counts[temp] += 1
  10.         return temp

  11.     def __setitem__(self, key, value):
  12.         if key in self.counts:
  13.             self.counts[key] += 1
  14.         else:
  15.             self.append(value)
  16.         super().__setitem__(key, value)

  17.     def __delitem__(self, instance): # 你的问题出现在这里(用 __delitem__)
  18.         self.counts.pop(super().__getitem__(instance))
  19.         super().__delitem__(instance)

  20.     def append(self, value):
  21.         self.counts[value] = 0
  22.         super().append(value)
  23.    
  24. c = CountList(1, 2, 3, 4, 5)

  25. del c[3]

  26. print(c.counts)

  27. print(c)
复制代码
  1. {1: 0, 2: 0, 3: 0, 5: 0}
  2. [1, 2, 3, 5]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-2 17:26:35 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-3 05:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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