如何使字典列表中的值唯一?

时间:2015-08-03 16:49:21

标签: python unique

我在Python中有一个字典列表,如下所示:

d = [{feature_a:1, feature_b:'Jul', feature_c:100}, {feature_a:2, feature_b:'Jul', feature_c:150}, {feature_a:1, feature_b:'Mar', feature_c:110}, ...]

我想要达到的目标是让feature_a_b_c保持唯一。

例如,如果我们有3个条目具有相同的feature_a_b,但有3个不同的值feature_c 100100150,然后在操作后,它应该是100150

我怎样才能做到这一点?

=============================================== ================= UPDATE:

好的,感谢Anand的出色答案,它完美无缺。但是,我还有一个问题。

假设我们有一个新的feature_d,字典看起来像:

d = [{feature_a:1, feature_b:'Jul', feature_c:100, feature_d:'A'}, {feature_a:2, feature_b:'Jul', feature_c:150, feature_d: 'B'}, {feature_a:1, feature_b:'Mar', feature_c:110, feature_d:'F'}, ...]

我只想对feature_a_b_c进行重复数据删除,但请保留feature_d。我怎样才能做到这一点?

非常感谢。

1 个答案:

答案 0 :(得分:5)

如果初始d列表的顺序不重要,您可以将每个字典的.items()转换为frozenset(),这是可以播放的,然后您可以将整个事物转换为set()frozenset(),然后将每个frozenset()转换回字典。示例 -

uniq_d = list(map(dict, frozenset(frozenset(i.items()) for i in d)))

sets()不允许重复元素。虽然你最终会失去列表的顺序。对于Python 2.x,不需要list(...),因为map()返回一个列表。

示例/演示 -

>>> import pprint
>>> pprint.pprint(d)
[{'feature_a': 1, 'feature_b': 'Jul', 'feature_c': 100},
 {'feature_a': 2, 'feature_b': 'Jul', 'feature_c': 150},
 {'feature_a': 1, 'feature_b': 'Mar', 'feature_c': 110},
 {'feature_a': 1, 'feature_b': 'Jul', 'feature_c': 100},
 {'feature_a': 1, 'feature_b': 'Jul', 'feature_c': 150}]
>>> uniq_d = list(map(dict, frozenset(frozenset(i.items()) for i in d)))
>>> pprint.pprint(uniq_d)
[{'feature_a': 1, 'feature_b': 'Jul', 'feature_c': 100},
 {'feature_a': 1, 'feature_b': 'Jul', 'feature_c': 150},
 {'feature_a': 1, 'feature_b': 'Mar', 'feature_c': 110},
 {'feature_a': 2, 'feature_b': 'Jul', 'feature_c': 150}]

对于新要求 -

  

但是,如果我有另一个feature_d但我只想重复删除feature_a,_b和_c

     

如果两个条目具有相同的feature_a,_b和_c,则它们被认为是相同且重复的,无论feature_d中有什么

执行此操作的一种简单方法是使用集合和新列表,仅添加集合所需的功能,并仅使用所需的功能进行检查。示例 -

seen_set = set()
new_d = []
for i in d:
    if tuple([i['feature_a'],i['feature_b'],i['feature_c']]) not in seen_set:
        new_d.append(i)
        seen_set.add(tuple([i['feature_a'],i['feature_b'],i['feature_c']]))

示例/演示 -

>>> d = [{'feature_a':1, 'feature_b':'Jul', 'feature_c':100, 'feature_d':'A'},
...  {'feature_a':2, 'feature_b':'Jul', 'feature_c':150, 'feature_d': 'B'},
...  {'feature_a':1, 'feature_b':'Mar', 'feature_c':110, 'feature_d':'F'},
...  {'feature_a':1, 'feature_b':'Mar', 'feature_c':110, 'feature_d':'G'}]
>>> seen_set = set()
>>> new_d = []
>>> for i in d:
...     if tuple([i['feature_a'],i['feature_b'],i['feature_c']]) not in seen_set:
...         new_d.append(i)
...         seen_set.add(tuple([i['feature_a'],i['feature_b'],i['feature_c']]))
...
>>> pprint.pprint(new_d)
[{'feature_a': 1, 'feature_b': 'Jul', 'feature_c': 100, 'feature_d': 'A'},
 {'feature_a': 2, 'feature_b': 'Jul', 'feature_c': 150, 'feature_d': 'B'},
 {'feature_a': 1, 'feature_b': 'Mar', 'feature_c': 110, 'feature_d': 'F'}]