title: How to count the number of times a string appears in a dictionary value in Python?
body: <p>So I have a dictionary that contains recipes. My current code is checking if a string exists in a value then add it to the counter. However I want it so that whenever the string is encountered in a dictionary value add one to the counter as there could be multiple occurrences of the same word in the value and I want to make sure I count all of them. Right now my code for counting the values in the dictionary is this:</p>
  <pre><code>        if (token in str(value).lower()) and (key == 'title'):
              title_c += 1
              
          elif (token in str(value).lower()) and (key == 'categories'):
              cat_c += 1
              
          elif (token in str(value).lower()) and (key == 'ingredients'):
              ing_c += 1
              
          elif (token in str(value).lower()) and (key == 'directions'):
              dire_c += 1
  </code></pre>
  <p>It checks the key for the dictionary and if the string appears in the value for that key and if it does add 1 to the counter. However if the token was 'banana' but the value for the title key for example was 'banana banana' it would add 1 to the title counter instead of 2. How could I change it for this?</p>
  <p>My dictionary contains a list of recipes. Here is an example of a piece of data in the dictionary. My search is only looking through title, categories, ingredients and directions.</p>
  <pre><code>{
    &quot;title&quot;: &quot;\&quot;Blanketed\&quot; Eggplant &quot;,
    &quot;categories&quot;: [
     &quot;Tomato&quot;,
     &quot;Vegetable&quot;,
     &quot;Appetizer&quot;,
     &quot;Side&quot;,
     &quot;Vegetarian&quot;,
     &quot;Eggplant&quot;,
     &quot;Pan-Fry&quot;,
     &quot;Vegan&quot;,
     &quot;Bon App\u00e9tit&quot;
    ],
    &quot;ingredients&quot;: [
     &quot;8 small Japanese eggplants, peeled&quot;,
     &quot;16 large fresh mint leaves&quot;,
     &quot;4 large garlic cloves, 2 slivered, 2 flattened&quot;,
     &quot;2 cups olive oil (for deep frying)&quot;,
     &quot;2 pounds tomatoes&quot;,
     &quot;7 tablespoons extra-virgin olive oil&quot;,
     &quot;1 medium onion, chopped&quot;,
     &quot;6 fresh basil leaves&quot;,
     &quot;1 tablespoon dried oregano&quot;,
     &quot;1 1/2 tablespoons drained capers&quot;
    ],
    &quot;directions&quot;: [
     &quot;Place eggplants on double thickness of paper towels. Salt generously. Let stand 1 hour. Pat dry with paper towels. Cut 2 deep incisions in each eggplant. Using tip of knife, push 1 mint leaf and 1 garlic sliver into each incision.&quot;,
     &quot;Pour 2 cups oil into heavy medium saucepan and heat to 375\u00b0F. Add eggplants in batches and fry until deep golden brown, turning occasionally, about 4 minutes. Transfer eggplants to paper towels and drain.&quot;,
     &quot;Blanch tomatoes in pot of boiling water for 20 seconds. Drain. Peel tomatoes. Cut tomatoes in half; squeeze out seeds. Chop tomatoes; set aside.&quot;,
     &quot;Heat 4 tablespoons extra-virgin olive oil in large pot over high heat. Add 2 flattened garlic cloves; saut\u00e9 until light brown, about 3 minutes. Discard garlic. Add onion; saut\u00e9 until translucent, about 5 minutes. Add reduced to 3 cups, stirring occasionally, about 20 minutes.&quot;,
     &quot;Mix capers and 3 tablespoons extra-virgin olive oil into sauce. Season with salt and pepper. Reduce heat. Add eggplants. Simmer 5 minutes, spooning sauce over eggplants occasionally. Spoon sauce onto platter. Top with eggplants. Serve warm or at room temperature.&quot;
    ],
    &quot;rating&quot;: 3.75,
    &quot;calories&quot;: 1386.0,
    &quot;protein&quot;: 9.0,
    &quot;fat&quot;: 133.0
   },
  
  {
    &quot;title&quot;: &quot;\&quot;Bloody Mary\&quot; Tomato Toast with Celery and Horseradish &quot;,
    &quot;categories&quot;: [
     &quot;Condiment/Spread&quot;,
     &quot;Tomato&quot;,
     &quot;Appetizer&quot;,
     &quot;Kid-Friendly&quot;,
     &quot;Quick &amp; Easy&quot;,
     &quot;Lunch&quot;,
     &quot;Spring&quot;,
     &quot;Summer&quot;,
     &quot;Vegetarian&quot;,
     &quot;Dairy Free&quot;,
     &quot;Peanut Free&quot;,
     &quot;Tree Nut Free&quot;,
     &quot;Soy Free&quot;,
     &quot;Kosher&quot;
    ],
    &quot;ingredients&quot;: [
     &quot;1 lemon, zested, juiced&quot;,
     &quot;1/2 shallot, finely chopped&quot;,
     &quot;1 pound grape tomatoes, halved&quot;,
     &quot;2 tablespoons extra-virgin olive oil&quot;,
     &quot;1 tablespoon Sherry vinegar&quot;,
     &quot;1 teaspoon kosher salt&quot;,
     &quot;1 teaspoon freshly ground black pepper&quot;,
     &quot;1 teaspoon Worcestershire sauce&quot;,
     &quot;1/4 teaspoon cayenne pepper&quot;,
     &quot;5 tablespoons finely grated fresh horseradish, divided&quot;,
     &quot;2 celery stalks, thinly sliced&quot;,
     &quot;3 tablespoons coarsely chopped celery leaves&quot;,
     &quot;6 tablespoons mayonnaise&quot;,
     &quot;6 slices toasted rye bread&quot;
    ],
    &quot;directions&quot;: [
     &quot;Combine lemon zest, lemon juice, shallot, tomatoes, oil, vinegar, salt, black pepper, Worcestershire sauce, cayenne, and 1 Tbsp. horseradish in a large bowl. Let marinate 10 minutes, then stir in celery stalks and leaves.&quot;,
     &quot;Meanwhile, combine mayonnaise and remaining 4 Tbsp. horseradish in a small bowl. Spread 1 Tbsp. horseradish-mayonnaise on each slice of rye. Top toasts evenly with tomato mixture.&quot;
    ],
    &quot;rating&quot;: 5.0,
    &quot;calories&quot;: 189.0,
    &quot;protein&quot;: 2.0,
    &quot;fat&quot;: 16.0
   },
  </code></pre>