Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18780 > unrolled thread

Re: generating unique set of dicts from a list of dicts

Started byDave Angel <d@davea.name>
First post2012-01-10 15:49 -0500
Last post2012-01-10 15:49 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: generating unique set of dicts from a list of dicts Dave Angel <d@davea.name> - 2012-01-10 15:49 -0500

#18780 — Re: generating unique set of dicts from a list of dicts

FromDave Angel <d@davea.name>
Date2012-01-10 15:49 -0500
SubjectRe: generating unique set of dicts from a list of dicts
Message-ID<mailman.4609.1326228576.27778.python-list@python.org>
On 01/10/2012 03:24 PM, bruce wrote:
> <SNIP>
> Since dict_hash returns a string, which is immutable, you can now use
> a dictionary to find the unique elements:
>
> uniques_map = {}
> for d in list_of_dicts:
>      uniques[dict_hash(d)] = d
> unique_dicts = uniques_map.values()
>
>>>>> *** not sure what the "uniqes" is, or what/how it should be defined....
Don't know about the rest of the message, but I think there's a typo in 
the above fragment.  On the third line, it should be uniques_map, not 
uniques that you're adding an item to.

And unless you have a really long (and strong) hash, you still have to 
check for actually equal.  In otherwords, the above solution will throw 
out a dict that happens to have the same hash as one already in the 
uniques_map.

Do you trust the  "equals" method  for your dicts ?  If not, that's your 
first problem.  If you do, then you can simply do

unique_dicts = []
for d in list_of_dicts:
      if d not in unique_dicts:
            unique_dicts.append(d)

Do it, then decide if performance is inadequate.  Only then  should you 
worry about faster methods, especially if the faster method is broken.



-- 

DaveA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web