擅长:python、mysql、java
<p>对于python 2或3,如何解决此任务应该没有区别,只需循环<code>Product_Titles</code>并将匹配项保存为返回值:</p>
<pre><code>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'], []]
</code></pre>
<p>如果任何一个习语不清楚,请随意询问它们的意思。另外,如果行空间很宝贵,则此解决方案也可以表示为列表理解:</p>
^{pr2}$
<p>这两者之间是有区别的,到目前为止,<code>break</code>在理解中并不存在。如果一个产品中存在多个品牌,它们将全部列在返回的列表中。这似乎是合理的,但实际上我会假设你也希望这种行为也在循环中。在</p>
<p>另外,请注意,<a href="https://google.github.io/styleguide/pyguide.html#List_Comprehensions" rel="nofollow noreferrer">the google python stylesheet</a>不鼓励使用超过一行(80个字符)的理解,而是使用循环。在</p>