簡體   English   中英

為Python中列表中的每個值返回字典中的鍵

[英]Returning keys in a dictionary for every value in a list in Python

所以我有一個已加載到字典中的 JSON 文件。 它有 8 個不同的鍵來存儲信息。 我正在嘗試創建一個搜索引擎,該引擎返回包含搜索字符串中所有單詞的食譜並返回它們。 我將字符串更改為將用於搜索的“令牌”列表。

下面是存儲在字典中的一些信息的示例。 只要標記位於標題、類別、成分或說明中,就應該返回食譜。

{
  "title": "101 \"Whaler\" Fish Sandwich ",
  "categories": [
   "Sandwich",
   "Cheese",
   "Dairy",
   "Fish",
   "Tomato",
   "Saut\u00e9",
   "Kid-Friendly",
   "Mayonnaise",
   "Cornmeal",
   "Lettuce",
   "Cookie"
  ],
  "ingredients": [
   "1 cup whole milk",
   "2 eggs",
   "1 1/2 cups flour",
   "1/4 cup yellow cornmeal",
   "2 tablespoons chopped parsley",
   "4 flounder fillets",
   "Salt and pepper to taste",
   "3 tablespoons canola oil",
   "4 sesame-seed hamburger buns",
   "4 leaves romaine lettuce",
   "1/2 tomato, sliced",
   "4 slices mild cheese, such as mild cheddar (optional)",
   "1/2 cup mayonnaise",
   "2 tablespoons pickle relish",
   "1 tablespoon lemon juice",
   "1 dash Tabasco sauce"
  ],
  "directions": [
   "1. In a medium-size bowl, whisk together the milk and eggs. In another medium-size bowl, mix together the flour, cornmeal, and parsley.",
   "2. Season the fish with the salt and pepper.",
   "3. Dredge the fish through the egg mixture, then coat it thoroughly with the flour mixture.",
   "4. In a large saut\u00e9 pan, immediately heat the oil over medium-high heat. When it is hot but not smoking, add the fillets to the pan. Cook on one side until the batter is light golden brown, about 4 minutes. Carefully turn the fillets and cook for 2 to 3 minutes more. Using a slotted spatula, remove them from the pan and drain on paper towels.",
   "5. Meanwhile, whisk together the tartar-sauce ingredients (if using).",
   "6. Slice the buns and spread the tartar sauce (if using) on the insides. Place a fillet on each bottom bun, then top with the lettuce, tomato, and cheese, if desired."
  ],
  "rating": 4.375,
  "calories": 819.0,
  "protein": 35.0,
  "fat": 42.0
 },

我如何能夠返回包含字符串中所有標記的字典中的鍵?

例如,如果有一個搜索字符串“煎餅糖漿”,我會在字典中返回包含“煎餅”和“糖漿”的食譜?

這段代碼從我讀入 Python 的 JSON 文件中創建了一個字典:

file = open('recipes.json') 
recipes = json.load(file)

此代碼清除輸入字符串

def tokenisation(input_string):
    
    #functions to remove digits and punctuation and replace it with whitespace
    d_translate = str.maketrans(string.digits, ' '*len(string.digits))
    p_translate = str.maketrans(string.punctuation, ' '*len(string.punctuation))
    
    #clean the string
    new_string = input_string.translate(d_translate)
    new_string = new_string.translate(p_translate)
    new_string = new_string.lower()
    
    #split the string
    splitted_string = new_string.split(" ")
    
    #make a list to store tokens in
    tokens = []
    
    #checking length of token
    for token in splitted_string:
        if len(token) > 3:
            tokens.append(token)
    
    return tokens

那么實際的搜索功能(到目前為止)是這樣的:

def search(query, ordering = 'normal', count = 10):
    

    token_list = tokenisation(query)    
    values = [recipes[k] for k in token_list]

但當然,它是自行檢查每個令牌,而不是所有存在的令牌。

我認為你想要的是以下內容:

def search(query, ordering = 'normal', count = 10):
    token_list = tokenisation(query)

    matching_recipes = []
    for recipe in recipes:
        recipe_tokens = []
        for key in recipe:
            if type(recipe[key]) != list:
                continue
            if type(recipe[key]) == str:
                recipe_tokens.append(recipe[key])
            for sentence in recipe[key]:
                # Make sure all the words from the recipes are in one big list
                recipe_tokens.extend([t for t in sentence.split()])

        if all([tl in recipe_tokens for tl in token_list]):
            # check if all the tokens from token_list are in the tokens of the recipe
            matching_recipes.append(recipe)

    return matching_recipes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM