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

我有一个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

我该如何实现?

================================================== =============更新:

好的,感谢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了。我该如何实现?

非常感谢。

回答:

如果初始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'}]

以上是 如何使字典列表中的值唯一? 的全部内容, 来源链接: utcz.com/qa/404505.html

回到顶部