Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18780
| Date | 2012-01-10 15:49 -0500 |
|---|---|
| From | Dave Angel <d@davea.name> |
| Subject | Re: generating unique set of dicts from a list of dicts |
| References | <CAP16ngr5C6tdY=3AHCFb=J2id-qF=75DXAmUcek2Ffet9y0ivQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4609.1326228576.27778.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: generating unique set of dicts from a list of dicts Dave Angel <d@davea.name> - 2012-01-10 15:49 -0500
csiph-web