I need to verify if there is a line where these 2 matches are met in the respective .txt :
- Line of the file named
file1.txtthat is equal to the input string1"old apple". - Line of the file named
file2.txtthat is equal to the input string2"on the floor".
For example, file1.txt have these lines:
juicy orange
old apple
rusty key
old apple
modern table
old apple
And for example, file2.txt have these other lines:
in a box
on the chair
on the bed
on the floor
on the floor
under the desktop
In this case the correct line would be line 4, that is, the one with index 3
This is my code but I am having problems with the mutual matching requirement
string1 = "old apple"
string2 = "on the floor"
num_linea_data_field_1 = None
num_linea_data_field_2 = None
validation_data_field_1 = False
validation_data_field_2 = False
with open("file1.txt","r+") as f:
lineas = [linea.strip() for linea in f.readlines()]
if (string1 not in lineas):
pass
else:
num_linea_data_field_1 = lineas.index(string1)
validation_data_field_1 = True
with open("file2.txt","r+") as f:
lineas = [linea.strip() for linea in f.readlines()]
if (string1 not in lineas):
pass
else:
num_linea_data_field_2 = lineas.index(string2)
validation_data_field_2 = True
if(validation_data_field_1 == True and validation_data_field_2 = True):
print("Mutual coincidence condition is satisfied with these inputs!")
else:
print("Mutual coincidence condition is not satisfied with these inputs!")
This code is failing in cases where one of the inputs matches but on different lines of the file.
How could I approach this double matching algorithm?