Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '21,': 0.07; 'skip:" 60': 0.07; 'subject:two': 0.07; '34,': 0.09; '46,': 0.16; '54,': 0.16; 'address)': 0.16; 'count.': 0.16; 'dict': 0.16; 'dictionary.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'hostname': 0.16; 'intersection': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'subject:compare': 0.16; 'subject:dictionaries': 0.16; 'wrote:': 0.18; '(the': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; 'entries': 0.24; 'simpler': 0.24; 'compare': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'matching': 0.30; 'subject:list': 0.30; 'skip:( 20': 0.30; "skip:' 10": 0.31; '25,': 0.31; 'doc': 0.31; 'keys': 0.31; 'problem': 0.35; 'created': 0.35; 'there': 0.35; 'set.': 0.36; '(i.e.,': 0.38; 'skip:m 40': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'skip:- 60': 0.39; 'either': 0.39; 'skip:x 10': 0.40; 'skip:o 30': 0.61; 'address': 0.63; 'between': 0.67; 'header:Reply-To:1': 0.67; 'containing': 0.69; 'reply-to:no real name:2**0': 0.71; 'skip:a 40': 0.72; 'difference.': 0.84; 'reply-to:addr:python.org': 0.84; '212': 0.91; '212,': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=bKAvfpOZ c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=LqG8EI-UCiYA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=6R-15RpTiGQA:10 a=5CKvRQiip-0VOSxdnF8A:9 a=F06f9rSepzHe1otz:21 a=DfmhnKXtVaBAPCLo:21 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Fri, 04 Oct 2013 17:41:36 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: compare two list of dictionaries References: <524DBB0B.3080008@mrabarnett.plus.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 140 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380904891 news.xs4all.nl 15953 [2001:888:2000:d::a6]:51485 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55485 On 04/10/2013 03:29, Mohan L wrote: [snip] > > 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'}] > > > > output2=(('INNCHN01','xxx.xx.xxx.11'), > ('HYDRHC02', 'xxx.xx.xxx.12'), > ('INNCHN03','xxx.xx.xxx.13'), > ('MUMRHC01','xxx.xx.xxx.14'), > ('n/a','xxx.xx.xxx.15'), > ('INNCHN05','xxx.xx.xxx.16'), > ('hostname1','n/a'), > ('hostname2','n/a'), > ('hostname10',''), > ('hostname8',''), > ('hostname200','xxx.xx.xxx.200'), > ('hostname300','xxx.xx.xxx.400'), > ) > > ## 1). > ## Create a dict from output1 in which the key is the ip and the value > is the count. > mongodb_data={} > for doc in output1: > mongodb_data.update({doc['ip']:doc['count']}) > A simpler way is: mongodb_data = {} for doc in output1: mongodb_data[doc['ip']] = doc['count'] > ## Create a set from output2 containing all the hostnames and ip_addrs. > all_hostname_ip_set=set(list(sum(output2, ()))) > > ## Get the intersection of the keys of the dict with the set. > key_set=set(mongodb_data.keys()) > int_keys=key_set & all_hostname_ip_set > # Print the entries of the dict for each member of the intersection. > print "-------------------intersection-----------------------------" > for key in int_keys: > print key,mongodb_data[key] > > ## 2). > ## Get the difference between the keys of the dict and the intersection. > deff_keys=key_set - all_hostname_ip_set > ## Print the entries of the dict for each member of the difference. > print "-------------------difference-------------------------------" > for key in deff_keys: > print key,mongodb_data[key] > > > $ ./demo.py > > -------------------intersection----------------------------- > 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 > hostname2 27 > hostname1 212 > hostname10 1 > hostname8 2 > -------------------difference------------------------------- > xxx.xx.xxx.29 3 > xxx.xx.xxx.28 10 > xxx.xx.xxx.17 11 > xxx.xx.xxx.18 2 > xxx.xx.xxx.19 19 > xxx.xx.xxx.23 43 > xxx.xx.xxx.22 31 > xxx.xx.xxx.25 80 > xxx.xx.xxx.24 46 > xxx.xx.xxx.27 90 > xxx.xx.xxx.26 91 > xxx.xx.xxx.2 4 > xxx.xx.xxx.21 25 > hostname3 513 > hostname4 98 > xxx.xx.xxx.4 10 > xxx.xx.xxx.20 21 > xxx.xx.xxx.1 3 > xxx.xx.xxx.3 8 > > > > 3). Ip address with is there only in output2 dictionary. > > xxx.xx.xxx.200 > xxx.xx.xxx.400 > > 1. Create a set from output2 containing all the ip_addrs > > > 2. Get the difference between the set and the keys of the dict created > from output1. > > > I have one problem here. I want to compare the output2 (hostname or ip > address) with output1 'ip' values (i.e., either hostname or ip address > of output2 does not matching the ip dict key in output1 then I want to > print either hostname or ip address from output2). > You already have the output1 'ip' values (the keys of mongodb_data) and the entries of output2 (all_hostname_ip_set). Just find the set difference.