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.

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to