从列表中的嵌套字典中删除重复项

时间:2009-08-14 19:41:27

标签: python dictionary

快速且非常基本的新手问题。

如果我有这样的词典列表:

L = []
L.append({"value1": value1, "value2": value2, "value3": value3, "value4": value4})

假设存在多个条目,其中value3和value4与其他嵌套字典相同。如何快速轻松地找到并删除那些重复的词典。

保留顺序并不重要。

感谢。

编辑:

如果有五个输入,例如:

L = [{"value1": fssd, "value2": dsfds, "value3": abcd, "value4": gk},
    {"value1": asdasd, "value2": asdas, "value3": dafdd, "value4": sdfsdf},
    {"value1": sdfsf, "value2": sdfsdf, "value3": abcd, "value4": gk},
    {"value1": asddas, "value2": asdsa, "value3": abcd, "value4": gk},
    {"value1": asdasd, "value2": dskksks, "value3": ldlsld, "value4": sdlsld}]

输出应该是这样的:

L = [{"value1": fssd, "value2": dsfds, "value3": abcd, "value4": gk},
    {"value1": asdasd, "value2": asdas, "value3": dafdd, "value4": sdfsdf},
    {"value1": asdasd, "value2": dskksks, "value3": ldlsld, "value4": sdlsld}

6 个答案:

答案 0 :(得分:7)

这是一种方式:

keyfunc = lambda d: (d['value3'], d['value4'])

from itertools import groupby
giter = groupby(sorted(L, key=keyfunc), keyfunc)

L2 = [g[1].next() for g in giter]
print L2

答案 1 :(得分:6)

在Python 2.6或3。*中:

import itertools
import pprint

L = [{"value1": "fssd", "value2": "dsfds", "value3": "abcd", "value4": "gk"},
    {"value1": "asdasd", "value2": "asdas", "value3": "dafdd", "value4": "sdfsdf"},
    {"value1": "sdfsf", "value2": "sdfsdf", "value3": "abcd", "value4": "gk"},
    {"value1": "asddas", "value2": "asdsa", "value3": "abcd", "value4": "gk"},
    {"value1": "asdasd", "value2": "dskksks", "value3": "ldlsld", "value4": "sdlsld"}]

getvals = operator.itemgetter('value3', 'value4')

L.sort(key=getvals)

result = []
for k, g in itertools.groupby(L, getvals):
    result.append(g.next())

L[:] = result
pprint.pprint(L)

在Python 2.5中几乎相同,除了你必须在追加中使用g.next()而不是next(g)。

答案 2 :(得分:2)

您可以使用临时数组来存储项目字典。之前的代码因为删除for循环中的项目而被窃听。

(v,r) = ([],[])
for i in l:
    if ('value4', i['value4']) not in v and ('value3', i['value3']) not in v:
        r.append(i)
    v.extend(i.items())
l = r

你的考试:

l = [{"value1": 'fssd', "value2": 'dsfds', "value3": 'abcd', "value4": 'gk'},
    {"value1": 'asdasd', "value2": 'asdas', "value3": 'dafdd', "value4": 'sdfsdf'},
    {"value1": 'sdfsf', "value2": 'sdfsdf', "value3": 'abcd', "value4": 'gk'},
    {"value1": 'asddas', "value2": 'asdsa', "value3": 'abcd', "value4": 'gk'},
    {"value1": 'asdasd', "value2": 'dskksks', "value3": 'ldlsld', "value4": 'sdlsld'}]

OUPUTS

{'value4': 'gk', 'value3': 'abcd', 'value2': 'dsfds', 'value1': 'fssd'}
{'value4': 'sdfsdf', 'value3': 'dafdd', 'value2': 'asdas', 'value1': 'asdasd'}
{'value4': 'sdlsld', 'value3': 'ldlsld', 'value2': 'dskksks', 'value1': 'asdasd'}

答案 3 :(得分:1)

for dic in list: 
  for anotherdic in list:
    if dic != anotherdic:
      if dic["value3"] == anotherdic["value3"] or dic["value4"] == anotherdic["value4"]:
        list.remove(anotherdic)

使用

进行测试
list = [{"value1": 'fssd', "value2": 'dsfds', "value3": 'abcd', "value4": 'gk'},
{"value1": 'asdasd', "value2": 'asdas', "value3": 'dafdd', "value4": 'sdfsdf'},
{"value1": 'sdfsf', "value2": 'sdfsdf', "value3": 'abcd', "value4": 'gk'},
{"value1": 'asddas', "value2": 'asdsa', "value3": 'abcd', "value4": 'gk'},
{"value1": 'asdasd', "value2": 'dskksks', "value3": 'ldlsld', "value4": 'sdlsld'}]

对我来说很好。:)

答案 4 :(得分:1)

这是一个字典的列表,但是,假设列表l中有更多字典:

l = [ldict for ldict in l if ldict.get("value3") != value3 or ldict.get("value4") != value4]

但这是你真正想做的吗?也许您需要优化您的描述。

顺便说一句,不要使用list作为名称,因为它是内置Python的名称。

编辑:假设您开始使用字典列表,而不是每个应该与您的示例一起使用的1个字典列表。如果其中任何一个值为None,它就不会起作用,所以更好的是:

l = [ldict for ldict in l if not ( ("value3" in ldict and ldict["value3"] == value3) and ("value4" in ldict and ldict["value4"] == value4) )]

但它似乎仍然是一种不寻常的数据结构。

编辑:无需使用明确的get s。

此外,解决方案总是存在权衡。如果没有更多信息并且没有实际测量,很难知道哪个性能权衡对于问题最重要。但是,正如Zen sez:“简单比复杂更好”。

答案 5 :(得分:0)

如果我理解正确,您想要丢弃原始列表中稍后的匹配但不关心结果列表的顺序,所以:

(用2.5.2测试)

tempDict = {}
for d in L[::-1]:
    tempDict[(d["value3"],d["value4"])] = d
L[:] = tempDict.itervalues()
tempDict = None