Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

python - How to make values in list of dictionary unique?

I have a list of dictionaries in Python, which looks like following:

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}, ...]

What I want to achieve is that to keep the feature_a, _b and _c unique.

For example, if we have 3 entries which have the same feature_a and _b, but have 3 different values of feature_c 100, 100, 150, then after the operation, it should be 100 and 150.

How can I achieve this?

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

OK, Thanks for Anand's excellent answer, it works perfectly. However, I have a further question.

Suppose we have a new feature_d and the dictionary looks like:

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'}, ...]

and I only want to deduplicate feature_a, _b and _c, but leave feature_d out. How can I achieve this?

Many thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If the order of the initial d list is not important , you can take the .items() of each dictionary and convert it into a frozenset() , which is hashable, and then you can convert the whole thing to a set() or frozenset() , and then convert each frozenset() back to dictionary. Example -

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

sets() do not allow duplicate elements. Though you would end up losing the order of the list. For Python 2.x , the list(...) is not needed, as map() returns a list.


Example/Demo -

>>> 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}]

For the new requirement -

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

A simple way to do this is to use a set and a new list, add only the features you need to the set, and check using only the features you need. Example -

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']]))

Example/Demo -

>>> 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'}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...