Reputation: 23
I have a list of lists and I need to find and print the list that contains full and partial matches using two conditions ignoring case.
l = [['2014','127.0.0.1','127','DNS sever Misconfiguration'],['2014','192.168.1.25','529','Failed logon for user user1'],['2014','127.0.0.1','1','This is a test message']]
Condition 1 and 2 can be anything, i.e. '192.186.1.25' or 'failed'
>>> for i in l:
if 'condition 1' in i and 'condition2' in i:
print i
gives ... nothing
I can use only one condition that has exact matching and get a result
>>> for i in l:
if '127.0.0.1' in i:
print i
['2014', '127.0.0.1', '127', 'DNS sever Misconfiguration']
['2014', '127.0.0.1', '1', 'This is a test message']
Any help would be appreciated
Upvotes: 2
Views: 2457
Reputation: 78690
Here's my attempt:
l = [['2014','127.0.0.1','127','DNS sever Misconfiguration'], ['2014','192.168.1.25','529','Failed logon for user user1'],['2014','127.0.0.1','1','This is a test message']]
condition1 = 'Failed'
condition2 = '2014'
for lst in l:
found_cond1 = False
found_cond2 = False
for string in lst:
if condition1 in string:
found_cond1 = True
if condition2 in string:
found_cond2 = True
if found_cond1 and found_cond2:
print(lst)
break
gives
['2014', '192.168.1.25', '529', 'Failed logon for user user1']
Upvotes: 0
Reputation: 12391
My guess is, you're just not matching the second condition properly e.g. if you do something like this:
'127.0.0.1' in i and 'Misconfiguration' in i
but i
looks like:
['2014', '127.0.0.1', '127', 'DNS sever Misconfiguration']
then '127.0.0.1'
will be in i
, but 'Misconfiguration'
won't - because it's a list, and in
for lists is exact match, but what you're looking for is a substring of an element of i
. If these are consistent, you can do something like:
'127.0.0.1' in i and 'Misconfiguration' in i[3]
or if they aren't, and you have to substring check all entries:
'127.0.0.1' in i and any('Misconfiguration' in x for x in i)
should do it. That will substring check each item in i
for your search term.
Upvotes: 2
Reputation: 122032
'condition 1' in i
will search for the string literal 'condition 1'
. Instead, I think you mean to search for the object referenced by the name condition1
, i.e.:
condition1 in l
If you want "partial" matches, use or
:
if condition1 in l or condition2 in l:
or any()
:
if any(cond in l for cond in (condition1, condition2)):
Upvotes: 1