gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:43:51 25 4
gpt4 key购买 nike

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

对于新需求——

However, what if that I have another feature_d but I only want to dedup feature_a, _b and _c

If two entries which have same feature_a, _b and _c, they are considered the same and duplicated, no matter what is in 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'}]

关于python - 如何使字典列表中的值唯一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31792680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com