![](/img/trans.png)
[英]How can i append values to an existing key in a dictionary in python? in a loop without " "
[英]Python: How can I append without overriding past append for loop
我目前正在嘗試在我的代碼中將 append 列為 output 查詢結果的 id。 我可以讓它執行其中一個 ID,但它會覆蓋第一個 ID 如何更改我的代碼以允許循環到output.append(q.id)
這是代碼:
@app.route('/new-mealplan', methods=['POST'])
def create_mealplan():
data = request.get_json()
recipes = data['recipes']
output = []
for recipe in recipes:
try:
query = Recipes.query.filter(func.lower(Recipes.recipe_name) == func.lower(recipe)).all()
# print(recipe)
if query:
query = Recipes.query.filter(func.lower(Recipes.recipe_name) == func.lower(recipe)).all()
for q in query:
output.append(q.id)
finally:
return jsonify({"data" : output})
為了解決這個問題,我刪除了Try
和Finally
塊。 然后在for循環完成后返回。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.