利用python检查产品名称列表是否有确切的品牌名称
时间: 2023-02-07 10:54:15 浏览: 75
你可以使用 Python 中的 in 运算符来检查一个品牌名称是否在产品名称列表中。例如:
```
product_names = ['Apple iPhone', 'Samsung Galaxy', 'Google Pixel']
brand_name = 'Apple'
if brand_name in product_names:
print('Brand found in product names')
else:
print('Brand not found in product names')
```
上面的代码将输出 "Brand found in product names",因为 "Apple" 这个品牌名称出现在了产品名称列表中。
注意,上面的代码只能检查品牌名称是否出现在产品名称的开头或者中间。如果你想要检查品牌名称是否出现在产品名称的任何位置,你可以使用 Python 中的正则表达式来匹配品牌名称。例如:
```
import re
product_names = ['Apple iPhone', 'Samsung Galaxy', 'Google Pixel']
brand_name = 'Apple'
for product_name in product_names:
if re.search(brand_name, product_name):
print('Brand found in product name:', product_name)
```
上面的代码将输出 "Brand found in product name: Apple iPhone",因为 "Apple" 这个品牌名称出现在了 "Apple iPhone" 这个产品名称中。