我正在尝试通过字典删除列表中的重复项:
def RemoveDuplicates(list):
d = dict()
for i in xrange(0, len(list)):
dict[list[i]] = 1 <------- error here
return d.keys()但它给我带来了以下错误:
TypeError: 'type' object does not support item assignment有什么问题吗?
发布于 2009-11-29 03:18:16
你应该写下:
d[list[i]] = 1但是为什么不这样做呢?
def RemoveDuplicates(l):
return list(set(l))此外,不要使用内置函数名作为变量名。它可能会导致混乱的bug。
发布于 2009-11-29 03:31:18
除了其他人所说的之外,这样做是不自然的:
for i in xrange(0, len(lst)):
do stuff with lst[i]当你可以这样做的时候:
for item in lst:
do stuff with item发布于 2009-11-29 03:19:02
dict是一种类型,你的意思是d[list[i]] = 1。
Addition:这指出了代码中的实际错误。但其他人提供的答案提供了更好的方法来实现你的目标。
https://stackoverflow.com/questions/1813469
复制相似问题