Path: csiph.com!usenet.pasdenom.info!news.izac.org!ohm.izac.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; '21,': 0.07; 'subject:two': 0.07; '34,': 0.09; '46,': 0.16; '54,': 0.16; 'count.': 0.16; 'dict': 0.16; 'dictionaries': 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; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:compare': 0.16; 'subject:dictionaries': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; 'entries': 0.24; 'compare': 0.26; 'code:': 0.26; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'appreciated.': 0.29; 'subject:list': 0.30; "skip:' 10": 0.31; '25,': 0.31; 'doc': 0.31; 'keys': 0.31; 'skip:d 20': 0.34; 'created': 0.35; 'received:84': 0.35; 'there': 0.35; 'really': 0.36; 'set.': 0.36; 'two': 0.37; 'list': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'address.': 0.39; 'either': 0.39; 'skip:x 10': 0.40; 'address': 0.63; 'dear': 0.65; 'between': 0.67; 'header:Reply-To:1': 0.67; 'below:': 0.68; 'containing': 0.69; 'reply-to:no real name:2**0': 0.71; '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=PIY2p5aC 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=tC_hH3zXU1mKWbR-JCwA:9 a=Kj4jTmIqpuRAxhOL:21 a=qkgO9Vf6TMYFDiwk:21 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Thu, 03 Oct 2013 19:44:27 +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: 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: 134 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380825854 news.xs4all.nl 15873 [2001:888:2000:d::a6]:49543 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55439 On 03/10/2013 17:11, 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 > 1. Create a dict from output1 in which the key is the ip and the value is the count. 2. Create a set from output2 containing all the hostnames and ip_addrs. 3. Get the intersection of the keys of the dict with the set. 4. Print the entries of the dict for each member of the intersection. > 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 > 1. Get the difference between the keys of the dict and the intersection. 2. Print the entries of the dict for each member of the difference. > 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. > Any help would be really appreciated. Thank you >