简体   繁体   English

如何在 Python 的字典键中查找多个关键字

[英]How Can I Find Multiple Keywords in Dictionary Keys in Python

I want to search a dictionary that has a title as the key, and a http link as the value assigned to that key.我想搜索一个以标题为键的字典,以及一个 http 链接作为分配给该键的值。 I wanna function that searches through the dictionary, searching for a key that contains all the keywords that i put into the function, and if it does not find any keys with the keywords, it returns nothing.我想要在字典中搜索的函数,搜索包含我放入函数的所有关键字的键,如果没有找到任何带有关键字的键,则不返回任何内容。 Here is the dict:这是字典:

I've tried if and in statements but nothing so far.我已经尝试过 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'}

Let's say I wanna search for black and ultra, the function would return the third item in the dictionary cause hoka one tor ultra high 2 wp boot - black' contains the keywords black and ultra.假设我想搜索 black 和 ultra,该函数将返回字典中的第三项,因为hoka one tor ultra high 2 wp boot - black'包含关键字 black 和 ultra。 If it doesn't contain all the keywords I put in, it would return nothing in the dictionary.如果它不包含我输入的所有关键字,则字典中将不返回任何内容。

You can iterate over the keys of a dictionary just like this:您可以像这样迭代字典的键:

for item in dic:
    if searchterm in item:
        print('do something')

Using list comprehension you can do something like this:使用列表理解,您可以执行以下操作:

def getUrl(keyword):
    return [dict[key] for key in dict.keys() if keyword in key]

if I call this with `keyword = 'black' it returns:如果我用 `keyword = 'black' 调用它,它会返回:

['https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black', 'https://kith.com/products/adidas-originals-yung-1-core-black-white']

This should return the list of urls corresponding to keys containing keyword .这应该返回与包含keyword键对应的 url 列表。

If you have more than one keyword this should do the trick:如果您有多个keyword这应该可以解决问题:

def getUrl(keywords):
    return [dict[key] for key in dict.keys() if len([keyword for keyword in keywords if keyword in key])>0]

if I call this with keywords = ['black','ultra'] it returns this:如果我用keywords = ['black','ultra']调用它,它会返回:

['https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black', 'https://kith.com/products/adidas-originals-yung-1-core-black-white']

They both return [] in case of no keys found.如果没有找到键,它们都返回[]

If you want to make a function that takes a list of keywords and checks that each keyword is represented in a value, you could do something like.如果您想创建一个接受关键字列表并检查每个关键字是否以值表示的函数,您可以执行类似的操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM