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


Groups > comp.lang.python > #55430

compare two list of dictionaries

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <l.mohanphy@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'output': 0.05; '21,': 0.07; 'subject:two': 0.07; '34,': 0.09; '46,': 0.16; '54,': 0.16; 'dictionaries': 0.16; 'dictionary.': 0.16; 'hostname': 0.16; 'subject:compare': 0.16; 'subject:dictionaries': 0.16; 'all,': 0.19; 'trying': 0.19; "skip:' 30": 0.19; 'print': 0.22; '31,': 0.24; 'compare': 0.26; 'code:': 0.26; 'tried': 0.27; 'appreciated.': 0.29; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "skip:' 10": 0.31; '25,': 0.31; 'doc': 0.31; 'to:name:python-list': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'thanks': 0.36; 'skip:4 10': 0.37; '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; '8bit%:6': 0.40; 'skip:x 10': 0.40; 'address': 0.63; 'dear': 0.65; 'below:': 0.68; '212': 0.91; '212,': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=ZiMe/1zea2HN63B/e4uTjvfBGXMI165Vb9gWY3UlyeE=; b=kdg2lu5WkbvfUvX/KyqfBO4D3qSMAwwWNA173a/Z7kXWPyz3XGZm8hctxsCG+ZNNM7 bz4MCDxVjGZ4eM9I0hARGt16zbdo6Ps60R0egbCngZjuS8nqqCe91t8xa3HszJvjWB0r FcBCNFdTkrW3b07E02wiVL/OR3guewTh5gTZB1Dmonvghifn21TnLT+K437BSJD2DL+R xldh0gWycxfb7fc8atU6YftcBORRdYaqwP1l43FEtQZi/mhMhxxgytFBJLq9Nq3UkZI0 LUvdIMiaU6cJyucfkEenKlfLcip92lpmk0RQiLI0W0wX3yhkZbyxSuqdfW+ZYFSt9Tne 3RIQ==
MIME-Version 1.0
X-Received by 10.180.189.17 with SMTP id ge17mr3143211wic.53.1380816690365; Thu, 03 Oct 2013 09:11:30 -0700 (PDT)
Date Thu, 3 Oct 2013 21:41:30 +0530
Subject compare two list of dictionaries
From Mohan L <l.mohanphy@gmail.com>
To python-list <python-list@python.org>
Content-Type multipart/alternative; boundary=001a11c22866544a7f04e7d87049
X-Mailman-Approved-At Thu, 03 Oct 2013 19:55:39 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.688.1380822940.18130.python-list@python.org> (permalink)
Lines 287
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380822940 news.xs4all.nl 16008 [2001:888:2000:d::a6]:44148
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55430

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

compare two list of dictionaries Mohan L <l.mohanphy@gmail.com> - 2013-10-03 21:41 +0530
  Re: compare two list of dictionaries Tobiah <toby@tobiah.org> - 2013-10-04 12:22 -0700

csiph-web