So i have a JSON file that is populated with recipes, a sample from the file:
{
"id": 6,
"name": "Lobster roll",
"type": "fish",
"ingredients":[
{"item": "Lobster","amount": 0.5},
{"item": "Baguette","amount": 8},
{"item": "Garlic","amount": 2}
]
},
{
"id": 7,
"name": "Potato and leaks soup",
"type": "vegetarian",
"ingredients":[
{"item": "Water","amount": 0.5},
{"item": "Potato","amount": 8},
{"item": "Onion","amount": 2}
]
}
What i want to achieve is to select random recipes from the JSON-file, for example 7 recipes. But with the condition that at least 25% of them should be of the type: fish. As every recipe is tagged with what type it is in the JSON-file.
I am using the random.sample() function, so how do i get the condition in play here?
import json
import random
with open('recipes.json') as json_file:
data = json.load(json_file)
random = random.sample(data, 7)
for i in range(0, len(random)):
print(random[i]["name"])
Aucun commentaire:
Enregistrer un commentaire