list1 = ['2.2.2.2', '5.5.5.5', '7.7.7.7', '11.11.11.11', '14.14.14.14']
list2 = ['1.1.1.1 cisco asa', '4.4.4.4 cisco 9k', '5.5.5.5 cisco nexus', '9.9.9.9 cisco 7k', '2.2.2.2 cisco 9500', '2.2.2.222 cisco 9300', '14.14.14.14 cisco 9200', '7.7.7.7 cisco 4121', '10.10.10.10 cisco 4100', '11.11.11.11 cisco asr', '100.100.100.100 cisco asr-x', '11.11.111.111 cisco asr-x-2']
########### lambda V3
print "V3"
for tintin in list1:
print filter(lambda a: tintin in a, list2)
OUTPUT
V3
['2.2.2.2 cisco 9500', '2.2.2.222 cisco 9300']
['5.5.5.5 cisco nexus']
['7.7.7.7 cisco 4121']
['11.11.11.11 cisco asr']
['14.14.14.14 cisco 9200']
Desired output
Would like to find the exact match… In the first output is giving me 2.2.222 instead of the single 2.2.2.2.
I want to match and compare list1 to list2 and match all the elements in list1 and output only the match in list2. However, I want the exact match. It’s not only matching 2.2.2.2 but is also matching 2.2.2.222 "Dont want to 2.2.2.222".
Thank you
>Solution :
Try to modify the lambda function as follows:
for tintin in list1:
print(list(filter(lambda a: tintin in a.split(), list2)))
So instead of checking if tintin is a sublist of a, you first get all the words in a (with a.split()) and then you search that list.
Output:
['2.2.2.2 cisco 9500']
['5.5.5.5 cisco nexus']
['7.7.7.7 cisco 4121']
['11.11.11.11 cisco asr']
['14.14.14.14 cisco 9200']