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


Groups > comp.lang.python > #18780

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.028
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'broken.': 0.07; 'cc:addr:tutor': 0.09; 'dict': 0.09; 'throw': 0.09; 'equal.': 0.16; 'immutable,': 0.16; 'methods,': 0.16; 'subject:set': 0.16; 'typo': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'subject:list': 0.21; 'header:In-Reply-To:1': 0.22; 'dictionary': 0.23; 'bruce': 0.24; 'do,': 0.25; '(and': 0.28; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'hash': 0.30; 'header:User-Agent:1': 0.33; 'actually': 0.33; 'decide': 0.33; 'rest': 0.35; 'especially': 0.35; 'unless': 0.35; 'problem.': 0.36; 'cc:2**1': 0.36; 'but': 0.37; 'received:192': 0.37; "there's": 0.37; 'think': 0.37; 'not,': 0.37; 'subject:from': 0.38; 'should': 0.39; 'third': 0.40; 'received:192.168': 0.40; 'happens': 0.40; 'unique': 0.61; 'your': 0.61; 'header:Reply- To:1': 0.71; 'reply-to:no real name:2**0': 0.72; '***': 0.73; '03:24': 0.84; 'elements:': 0.84
Date Tue, 10 Jan 2012 15:49:27 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15
MIME-Version 1.0
To bruce <badouglas@gmail.com>
Subject Re: generating unique set of dicts from a list of dicts
References <CAP16ngr5C6tdY=3AHCFb=J2id-qF=75DXAmUcek2Ffet9y0ivQ@mail.gmail.com>
In-Reply-To <CAP16ngr5C6tdY=3AHCFb=J2id-qF=75DXAmUcek2Ffet9y0ivQ@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:tsFbsVv4AZQGUYh4uxWOHIlDmN7tatqdLHJmP/yRUB8 cluS+npOQjLTwh9Ieb6KuYH+4b0TFD3PuiziDl7eax1dOcR3v4 WU3NRUOf+TIbB1H8W2/mcoUbxXi5seSHWe/fT5v4OXiox45W9c bq0OLHXcUqgYmE75GlgXqvI0BQyu/ltqiegU88GlgxPVCeVD0y RyCuxi8FsYgzXPIOwQOfDE1VZiWuHr0uPHK8OHFyb7ZcWgSllp tIA30nVQZk6xI871wBuFMA2oEFHCK73rB0iwGvdkWcVyL6ZkP0 u18CauNcN+dqGGVrgARxXhoCOhGe8gcQIp4exoKMMsHhbx8eo1 RXE5zGGHdyF+fRdOAfo8=
Cc python-list@python.org, tutor@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4609.1326228576.27778.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326228576 news.xs4all.nl 6972 [2001:888:2000:d::a6]:51412
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18780

Show key headers only | View raw


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


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