Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!fx27.iad.POSTED!not-for-mail Message-ID: <524F1574.4030900@tobiah.org> From: Tobiah User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 Newsgroups: comp.lang.python Subject: Re: compare two list of dictionaries References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Lines: 137 X-Complaints-To: abuse@teranews.com NNTP-Posting-Date: Fri, 04 Oct 2013 19:22:29 UTC Organization: TeraNews.com Date: Fri, 04 Oct 2013 12:22:28 -0700 X-Received-Bytes: 4837 Xref: csiph.com comp.lang.python:55490 On 10/03/2013 09:11 AM, Mohan L wrote: > Dear All, > > I have two list of dictionaries like below: > > In the below dictionaries the value of ip can be either hostname or ip address. > > output1=[ > {'count': 3 , 'ip': 'xxx.xx.xxx.1'}, > {'count': 4, 'ip': 'xxx.xx.xxx.2'}, > {'count': 8, 'ip': 'xxx.xx.xxx.3'}, > {'count': 10, 'ip': 'xxx.xx.xxx.4'}, > {'count': 212, 'ip': 'hostname1'}, > {'count': 27, 'ip': 'hostname2'}, > {'count': 513, 'ip': 'hostname3'}, > {'count': 98, 'ip': 'hostname4'}, > {'count': 1, 'ip': 'hostname10'}, > {'count': 2, 'ip': 'hostname8'}, > {'count': 3, 'ip': 'xxx.xx.xxx.11'}, > {'count': 90, 'ip': 'xxx.xx.xxx.12'}, > {'count': 12, 'ip': 'xxx.xx.xxx.13'}, > {'count': 21, 'ip': 'xxx.xx.xxx.14'}, > {'count': 54, 'ip': 'xxx.xx.xxx.15'}, > {'count': 34, 'ip': 'xxx.xx.xxx.16'}, > {'count': 11, 'ip': 'xxx.xx.xxx.17'}, > {'count': 2, 'ip': 'xxx.xx.xxx.18'}, > {'count': 19, 'ip': 'xxx.xx.xxx.19'}, > {'count': 21, 'ip': 'xxx.xx.xxx.20'}, > {'count': 25, 'ip': 'xxx.xx.xxx.21'}, > {'count': 31, 'ip': 'xxx.xx.xxx.22'}, > {'count': 43, 'ip': 'xxx.xx.xxx.23'}, > {'count': 46, 'ip': 'xxx.xx.xxx.24'}, > {'count': 80, 'ip': 'xxx.xx.xxx.25'}, > {'count': 91, 'ip': 'xxx.xx.xxx.26'}, > {'count': 90, 'ip': 'xxx.xx.xxx.27'}, > {'count': 10, 'ip': 'xxx.xx.xxx.28'}, > {'count': 3, 'ip': 'xxx.xx.xxx.29'}] > > > In the below dictionaries have either hostname or ip or both. > > output2=( > > {'hostname': 'INNCHN01', 'ip_addr': 'xxx.xx.xxx.11'}, > {'hostname': 'HYDRHC02', 'ip_addr': 'xxx.xx.xxx.12'}, > {'hostname': 'INNCHN03', 'ip_addr': 'xxx.xx.xxx.13'}, > {'hostname': 'MUMRHC01', 'ip_addr': 'xxx.xx.xxx.14'}, > {'hostname': 'n/a', 'ip_addr': 'xxx.xx.xxx.15'}, > {'hostname': 'INNCHN05', 'ip_addr': 'xxx.xx.xxx.16'}, > {'hostname': 'hostname1', 'ip_addr': 'n/a'}, > {'hostname': 'hostname2', 'ip_addr': 'n/a'}, > {'hostname': 'hostname10', 'ip_addr': ''}, > {'hostname': 'hostname8', 'ip_addr': ''}, > {'hostname': 'hostname200', 'ip_addr': 'xxx.xx.xxx.200'}, > {'hostname': 'hostname300', 'ip_addr': 'xxx.xx.xxx.400'}, > > ) > > trying to get the following difference from the above dictionary > > 1). compare the value of 'ip' in output1 dictionary with either 'hostname' and 'ip_addr' output2 dictionary and print their > intersection. Tried below code: > > > for doc in output1: > for row in output2: > if((row["hostname"] == doc["ip"]) or (row["ip_addr"] == doc["ip"])): > print doc["ip"],doc["count"] > > *output:* > hostname1 212 > hostname2 27 > hostname10 1 > hostname8 2 > xxx.xx.xxx.11 3 > xxx.xx.xxx.12 90 > xxx.xx.xxx.13 12 > xxx.xx.xxx.14 21 > xxx.xx.xxx.15 54 > xxx.xx.xxx.16 34 > > 2). need to print the below output if the value of 'ip' in output1 dictionary is not there in in output2 dictionary(ip/hostname > which is there in output1 and not there in output2): > > xxx.xx.xxx.1 3 > xxx.xx.xxx.2 4 > xxx.xx.xxx.3 8 > xxx.xx.xxx.4 10 > hostname3 513 > hostname4 98 > xxx.xx.xxx.17 11 > xxx.xx.xxx.18 2 > xxx.xx.xxx.19 19 > xxx.xx.xxx.20 21 > xxx.xx.xxx.21 25 > xxx.xx.xxx.22 31 > xxx.xx.xxx.23 43 > xxx.xx.xxx.24 46 > xxx.xx.xxx.25 80 > xxx.xx.xxx.26 91 > xxx.xx.xxx.27 90 > xxx.xx.xxx.28 10 > xxx.xx.xxx.29 3 > > 3). Ip address with is there only in output2 dictionary. > > xxx.xx.xxx.200 > xxx.xx.xxx.400 > > Any help would be really appreciated. Thank you > > Thanks > Mohan L There is a bit of work that can be consolidated to help both case 2 and 3. Then both cases are a little more straightforward: ################################################## import itertools inside_out = {x['ip']: x for x in output1} set1 = set(inside_out.keys()) ### CASE 2 ### set2 = set(itertools.chain.from_iterable([x.values() for x in output2])) for name in set1 - set2: print name, inside_out[name]['count'] ### CASE 3 ### set2 = set([x['ip_addr'] for x in output2]) print "\n".join(set2 - set1)