I have a 2 lists with data which I want to match and receive 3rd list with 'True' or 'False'. However I need to create a special match. See my code below.
import pandas as pd
list1 = ['food', 'food', 'item']
list2 = ['apple fr', 'cherry fr', 'banana fr']
my_list1 = list(map(str, list1))
my_list2 = list(map(str, list2))
for i in range(len(my_list2)):
if my_list2[i] in [s for s in my_list2 if "fr" in s]:
my_list2[i] = 'food'
result = []
for i in range(int(len(my_list1))):
result.append(my_list1[i] == my_list2[i])
my_data1 = pd.DataFrame({'a': list1, 'b': list2, 'check': result})
print(my_data1)
Output:
a b check
0 food apple fr True
1 food cherry fr True
2 item banana fr False
Basically, I want to match elements of list using next pattern: if elem from list2 has 'fr' than it should return True on 'food' element from list1.
As you can see I already have a sort of solution where I am copying lists , editing them and comparing edited copies. However this approach is not the best since it is really memory consuming. Maybe it is possible to play with 'if' condition rather then manipulating data. Is there is any better way to do it? Thanks