Search for Multiple Elements in Same Sublist of List
I am trying to get Python to search my list for a sublist which contains
both of my search terms, but instead I get any sublist which contains one
or more of the searches. Here is my code:
search1 = 3
search2 = 4
data = [[4,3],[4,7], [6,3], [9,2]]
found = False
for sublist in data:
if search1 in sublist:
print("there", sublist)
if search2 in sublist:
print("there", sublist)
found = True
if(found==False):
print("not there")
print(data)
if(found==False):
print("not there")
print(data)
The output I get for this is:
there [4,3] #I only want this sublist, and only once.
there [4,3]
there [6,3] #I don't want this sublist.
Cheers! 5813
No comments:
Post a Comment