如何在 Python 中的字典键中查找多个关键字
How Can I Find Multiple Keywords in Dictionary Keys in Python
我想搜索以标题为键、以 http link 作为分配给该键的值的字典。我想要搜索字典的函数,搜索包含我放入函数中的所有关键字的键,如果它没有找到任何包含关键字的键,它 return 什么都没有。这是字典:
我已经尝试过 if 和 in 语句,但到目前为止还没有。
dict = {
'adidas originals yung-1 - core black / white':
'https://kith.com/products/adidas-originals-yung-1-core-black-white',
'adidas originals yung-1 - grey one / white':
'https://kith.com/products/adidas-originals-yung-1-grey-one-white',
'hoka one tor ultra high 2 wp boot - black':
'https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black'}
假设我想搜索 black 和 ultra,该函数将 return 字典中的第三项导致 hoka one tor ultra high 2 wp boot - black'
包含关键字 black 和 ultra。如果它不包含我输入的所有关键字,它将 return 字典中没有任何内容。
您可以像这样遍历字典的键:
for item in dic:
if searchterm in item:
print('do something')
使用列表理解你可以做这样的事情:
def getUrl(keyword):
return [dict[key] for key in dict.keys() if keyword in key]
如果我用 `keyword = 'black' 调用它 returns:
['https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black', 'https://kith.com/products/adidas-originals-yung-1-core-black-white']
这应该 return 对应于包含 keyword
.
键的 url 列表
如果您有多个 keyword
,这应该可以解决问题:
def getUrl(keywords):
return [dict[key] for key in dict.keys() if len([keyword for keyword in keywords if keyword in key])>0]
如果我用 keywords = ['black','ultra']
调用它,它 return 就是这样的:
['https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black', 'https://kith.com/products/adidas-originals-yung-1-core-black-white']
他们都 return []
以防找不到钥匙。
如果你想创建一个函数来获取关键字列表并检查每个关键字是否在一个值中表示,你可以做类似的事情。
keywords = ['black', 'ultra']
def dict_search(list_of_keywords):
for key in dict.keys():
if all(x in key for x in list_of_keywords):
return(key)
In [1]: dict_search(keywords)
hoka one tor ultra high 2 wp boot - black
我想搜索以标题为键、以 http link 作为分配给该键的值的字典。我想要搜索字典的函数,搜索包含我放入函数中的所有关键字的键,如果它没有找到任何包含关键字的键,它 return 什么都没有。这是字典:
我已经尝试过 if 和 in 语句,但到目前为止还没有。
dict = {
'adidas originals yung-1 - core black / white':
'https://kith.com/products/adidas-originals-yung-1-core-black-white',
'adidas originals yung-1 - grey one / white':
'https://kith.com/products/adidas-originals-yung-1-grey-one-white',
'hoka one tor ultra high 2 wp boot - black':
'https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black'}
假设我想搜索 black 和 ultra,该函数将 return 字典中的第三项导致 hoka one tor ultra high 2 wp boot - black'
包含关键字 black 和 ultra。如果它不包含我输入的所有关键字,它将 return 字典中没有任何内容。
您可以像这样遍历字典的键:
for item in dic:
if searchterm in item:
print('do something')
使用列表理解你可以做这样的事情:
def getUrl(keyword):
return [dict[key] for key in dict.keys() if keyword in key]
如果我用 `keyword = 'black' 调用它 returns:
['https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black', 'https://kith.com/products/adidas-originals-yung-1-core-black-white']
这应该 return 对应于包含 keyword
.
如果您有多个 keyword
,这应该可以解决问题:
def getUrl(keywords):
return [dict[key] for key in dict.keys() if len([keyword for keyword in keywords if keyword in key])>0]
如果我用 keywords = ['black','ultra']
调用它,它 return 就是这样的:
['https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black', 'https://kith.com/products/adidas-originals-yung-1-core-black-white']
他们都 return []
以防找不到钥匙。
如果你想创建一个函数来获取关键字列表并检查每个关键字是否在一个值中表示,你可以做类似的事情。
keywords = ['black', 'ultra']
def dict_search(list_of_keywords):
for key in dict.keys():
if all(x in key for x in list_of_keywords):
return(key)
In [1]: dict_search(keywords)
hoka one tor ultra high 2 wp boot - black