Reputation: 59
I have a matrix(l2) and a list(l1). I want to check if any word of the list(elements in l1) are in the matrix or not. And If there is, the algorithm should attach the separated words. For example,
l1=['soheila','mahin','rose','pari']
l2=[['so he ila ha','soh e i la h a','so h eil a ha'],
['ma h in','m ah in','mahi n'],
['r os es', 'ro se s','ros e s'],
['pa ri sa','pari s a','p ar i sa']]
and my desire output is:
output=[['soheila ha','soheila h a','soheila ha'],
['mahin','mahin','mahin'],
['roses', 'rose s','rose s'],
['pari sa','pari s a','pari sa']]
As you see in this example, the spaces between the character of the words in the matrix and the list are deleted but the rest of words no. I use this code to delete all of the spaces between characters in the matrix:
import numpy.core.defchararray as np_f
l3=list()
l3=l2
new_data = np_f.replace(l3, ' ', '')
#print(new_data)
for count_i, value in enumerate(l2):
result = [i for i in l3[count_i] and new_data[count_i] if l1[count_i] in i]
print(result)
But I don't know how to code the condition of the problem to produce the output matrix. I search alot and I know I can do it by using equalsIgnoreCase
in Java but I'm at the beginner level of Python and have weaknesses. Could you help me to solve this problem and produce the output matrix?
Upvotes: 0
Views: 78
Reputation: 11
If you don’t mind my asking why not just split each index in the L2 list via the spaces:
L3=[]
For i in L2:
L3.append("".join(i .split(" ")))
Then check normally if a work in L1
is in L3
Haven’t checked if this would work but this is what I thought of, off the top of my head.
Upvotes: 0
Reputation: 46
You can do it like this
l1=['soheila','mahin','rose','pari']
l2=[['so he ila ha','soh e i la h a','so h eil a ha'],
['ma h in','m ah in','mahi n'],
['r os es', 'ro se s','ros e s'],
['pa ri sa','pari s a','p ar i sa']]
l = [[ word + " " + subword.replace(' ', '').replace(word, '') for subword in sublist if word in subword.replace(' ', '')] for sublist in l2 for word in l1]
result = list(filter(None, l))
print(result)
Output:
[['soheila ha', 'soheila ha', 'soheila ha'], ['mahin ', 'mahin ', 'mahin '], ['rose s', 'rose s', 'rose s'], ['pari sa', 'pari sa', 'pari sa']]
Upvotes: 2