-6

I have two lists of different sizes. One has Product Titles and other one has brand names ( may be one word or more). I need to check if the Product Title has exact brand name(present in brand list) mentioned and extract the same else return empty list. Am facing issue in extracting matching Brand name because brand may contain multiple words.

For eg. : Following is the input:

Product_Titles =[['Best abc def hair oil'],['laptop erg eds ram 15 GB'],['oops dfr watch']]

Brand_List = [['abc def'],['dfe sdf sd'],['erg eds']]

#Expected Output :
Brand = [['abc def'],['erg eds'],[]]

Getting an empty list for third Product Title because we were not able to get any matching brand with Brand_List.

P.S. :

Only if the complete Brand Name matches then we should return Brand Name.

I have tried Regex but it's not working because if we have 'str' in brand list and 'string' in Product Titles, it will give 'string' as Brand. But I need exact output.

Thanks a ton for all wonderful answers ! I have combined all the below suggestions and came up with my version of the same.

Solution :

Solution Code

Abhay Raj Singh
  • 169
  • 2
  • 6
  • 4
    Show us what you've tried, please. – wholevinski Feb 15 '18 at 14:18
  • 4
    "Please code in Python 3" Stack Overflow is not a "coding factory". You need to code yourself. Show us what you have done, where it goes wrong... you need to show your efforts. – Edwin van Mierlo Feb 15 '18 at 14:23
  • Please take a look here: https://stackoverflow.com/help/mcve and then edit your question to improve it. – progmatico Feb 15 '18 at 14:24
  • 1
    I'm voting to close this question as off-topic because there isn't even code posted. It looks like OP is hoping someone will write the code on their behalf. – dbeer Feb 15 '18 at 23:02
  • [Consider accepting a proposed answer instead of including your answer in the question.](https://stackoverflow.com/help/someone-answers) – Arne Feb 16 '18 at 12:22

2 Answers2

0

There should be no difference in how this task can be solved for python 2 or 3, simply loop over the Product_Titles and save matches to a return value:

Product_Titles =[['Best abc def hair oil'],['laptop erg eds ram 15 GB'],['oops dfr watch']]
Brand_List = [['abc def'],['dfe sdf sd'],['erg eds']]
ret = []

for product in Product_Titles:
  for brand in Brand_List:
    if brand[0] in product[0]:
      ret.append(brand)
      break
  else:
    ret.append([])
print(ret)
>> [['abc def'], ['erg eds'], []]

If any of the idioms are unclear, feel free to ask what they mean. Also, if line space is precious, this solution can be expressed as a list comprehension as well:

ret = [brand[0] for brand in Brand_List if brand[0] in prod[0]] for prod in Product_Titles]
print(ret)
>> [['abc def'], ['erg eds'], []]

There is a difference between these two, in so far that the break does not exist in the comprehension. If multiple brands exist in a single product, they will all be listed in the returned list. Which seems reasonable though, I would actually assume that you'd want this behavior in the loop as well.

Also, note that the google python stylesheet discourages using comprehensions that are longer than one line (80 characters), and to use loops instead.

Arne
  • 17,706
  • 5
  • 83
  • 99
  • What if brand comes in mid of the Product Title ? How do we solve it then ? Edited the question. – Abhay Raj Singh Feb 16 '18 at 09:36
  • That's not a problem. The `in` keyword, [which performs the check](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method), doesn't care where in the string the substring occurs. – Arne Feb 16 '18 at 10:04
  • Thanks Arne. It was of great help ! – Abhay Raj Singh Feb 16 '18 at 11:43
  • @AbhayRajSingh If this or another answer has helped you, consider upvoting or accepting it to indicate to future readers whether it is a solution to the problem =) – Arne Feb 16 '18 at 12:15
0

You can use a list comprehension:

Product_Titles =[['abc def hair oil'],['erg eds laptop'],['oops dfr watch']]
Brand_List = [['abc def'],['dfe sdf sd'],['erg eds']] 
titles = [[[i] if b.startswith(i) else [] for [i] in Brand_List] for [b] in Product_Titles]
final_titles = [filter(None, i)[0] if any(i) else [] for i in titles]

Output:

[['abc def'], ['erg eds'], []]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • ah... you should have done it in a one liner ! :-) :-) `final_titles = [filter(None, a)[0] if any(a) else [] for a in [[[i] if b.startswith(i) else [] for [i] in Brand_List] for [b] in Product_Titles]]` – Edwin van Mierlo Feb 15 '18 at 14:53
  • What if brand comes in mid of the Product Title ? How do we solve it then ? Edited the question. Sorry – Abhay Raj Singh Feb 16 '18 at 09:28