小编Int*_*ba5的帖子

Python dict 的 __new__ 方法的问题

嗨,我正在上这门课

class multiset(dict):

  def __new__(cls,iterabile):
      d = dict()
      for i in iterabile:
          if i not in d.keys():
              d[i] = iterabile.count(i)
      return super().__new__(cls,d)
Run Code Online (Sandbox Code Playgroud)

此类是一个自定义字典,它从输入列表创建一个字典,其中键是元素,值是键元素在列表中出现的次数。问题是 super().__new__(cls,d) 返回此错误:

回溯(最近一次调用最后一次):

文件“”,第 1 行,m = multiset([1,1,1,2,1,3,2,3])

类型错误:无法将字典更新序列元素 #0 转换为序列

python dictionary

2
推荐指数
1
解决办法
671
查看次数

标签 统计

dictionary ×1

python ×1