Python: How to remove all duplicate items from a list
嘿伙计们
我有一个(文件,inode,图像,哈希)-tuples的列表。如果它们具有相同的哈希,我需要删除两个项目。我没有那么多的编程经验,所以也许我必须看到的提示已经有所帮助。 我已经搜索了互联网,但我发现的唯一的事情是this。 到目前为止,我已经提出了这个(非常尴尬)的解决方案:
hashlist = {}
files_tobe_removed = []
for (file, inode, image, hash) in self.files_for_json:
hashlist[hash] = 0
for (file, inode, image, hash) in self.files_for_json:
hashlist[hash] +=1
for (k,v) in hashlist.iteritems():
if v == 2:
files_tobe_removed.append(k)
for (file,inode,image,hash) in self.files_for_json:
if hash in files_tobe_removed:
path = self.outDir + file
os.remove(path)
self.files_for_json.remove((file,inode,image,hash))
任何帮助将不胜感激。提前致谢
答案 0 :(得分:1)
>>> from collections import Counter
>>> L=[1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,1,2,3]
>>> [k for k,v in Counter(L).items() if v==1]
[7, 8, 9]
澄清:
hash_counter = Counter(x[3] for x in self.files_for_json)
for (file,inode,image,hash) in self.files_for_json:
if hash_counter[hash]>1:
# duplicated hash
...