Skip to main content

Searching and replacing element of a list from another list


I have asked a question here which was answered. In this question I want to generalise my case to dealwith string of words. Suppose I have the following lists:


l = {"ONIONTOMATO", "BUTTERMILK", "BUTTERWHEAT"};
ll = {{"butter", "cinnamon", "egg", "milk", "starch", "vanilla",
"wheat"}, {"bay", "beef", "black_pepper", "cane_molasses", "lamb",
"onion", "potato", "tamarind", "tomato", "vegetable_oil",

"vinegar", "wheat"}}

I want to look at each l ie l[[1]], l[[2]] and l[[3]] and make a replacement in ll. So that I get 3 lists. For l[[1]]:


{{"butter", "cinnamon", "egg", "milk", "starch", "vanilla", 
"wheat"}, {"bay", "beef", "black_pepper", "cane_molasses", "lamb",
"ONIONTOMATO", "potato", "tamarind", "vegetable_oil",
"vinegar", "wheat"}}

For l[[2]]:


{{"BUTTERMILK", "cinnamon", "egg", "starch", "vanilla", 

"wheat"}, {"bay", "beef", "black_pepper", "cane_molasses", "lamb",
"onion", "potato", "tamarind", "tomato", "vegetable_oil",
"vinegar", "wheat"}}

And for l[[3]]:


{{"BUTTERWHEAT", "cinnamon", "egg", "milk", "starch", "vanilla", 
}, {"bay", "beef", "black_pepper", "cane_molasses", "lamb",
"onion", "potato", "tamarind", "tomato", "vegetable_oil",
"vinegar", "wheat"}}


If words in l were characters, as in the previous question, one could use


replace = Replace[ll, {OrderlessPatternSequence[ a___, ## & @@ ToLowerCase[Characters@#], b___]} :> {a, #, b}, \[Infinity]] &;

But I am not sure how to proceed in this case. I have tried to change Characters to String in definition of replace but this did not work.


Edit: Please note that my original ll size has length of 56498 and my l has length of 72390 this methods seems only work for my example. So naturally I am looking for an answer which can be applied.




Comments