我刚开始尝试使用python,现在我处于一种两难的境地。
我正在尝试从json文档打印,而我只是获取数组中的最后一个元素。
[{
"FullMeasure": "1/2 cup",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 1142,
"IngredientName": "frozen corn",
"IngredientStep": 10,
"LanguageId": 0,
"PostPreparation": ", thawed, drained",
"PrePreparation": "",
"QuantityNum": 0,
"QuantityText": "1/2",
"QuantityUnit": "cup",
"RecipeIngredientID": 6291555,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "1/4 cup",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 1523,
"IngredientName": "red pepper",
"IngredientStep": 20,
"LanguageId": 0,
"PostPreparation": "s",
"PrePreparation": "chopped",
"QuantityNum": 0,
"QuantityText": "1/4",
"QuantityUnit": "cup",
"RecipeIngredientID": 6291554,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "2 Tbsp.",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 20197,
"IngredientName": "chopped green chiles",
"IngredientStep": 30,
"LanguageId": 0,
"PostPreparation": ", drained",
"PrePreparation": "canned ",
"QuantityNum": 2,
"QuantityText": "2",
"QuantityUnit": "Tbsp.",
"RecipeIngredientID": 6291552,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 19682,
"IngredientName": "KRAFT DELI DELUXE Process American Cheese Slice",
"IngredientStep": 80,
"LanguageId": 0,
"PostPreparation": "s",
"PrePreparation": "",
"QuantityNum": 4,
"QuantityText": "4",
"QuantityUnit": "",
"RecipeIngredientID": 6291558,
"TrialMeasure": "",
"TrialWeight": ""
}
]
我想得到所有的构成ID,所以我写了这段小代码来抓住IngredientID
rec = recipes['Recipes'][0]['Ingredients']
for records in rec:
value = {
'Ingredient ID': records['IngredientID']
}
当我返回值时,我得到
{
"Ingreient ID": 19682
}
我正在尝试获取每个元素的成分ID,而我似乎无法弄明白。任何指针都将受到赞赏。谢谢
答案 0 :(得分:0)
你现在正在做的是你每次循环都重新定义value
。您需要在循环之前定义value
并将其分配给您可以添加的空列表。
value = {'Ingredient ID':[]}
for records in rec:
value['Ingredient ID'].append(records['IngredientID'])
您还可以将值定义为如下列表:
value = []
for records in rec:
value.append(records['IngredientID'])
答案 1 :(得分:0)
你是overwriting
循环内的值。因此,最终结果将是json中的last
ingredient_id。请尝试将值添加到list
:
myList = []
rec = recipes['Recipes'][0]['Ingredients']
for records in rec:
value = {
'Ingredient ID': records['IngredientID']
}
myList.append(value)
myList
将拥有所有成分
您甚至可以使用 一行 来执行此操作:
[r['IngredientID'] for r in rec]
这将列出['1142',1523,..]
答案 2 :(得分:0)
您每次都在重新分配变量value
。因此,你得到了最后一个元素。相反,你应该尝试
# Your code
value = []
for records in rec:
value.append(records['IngredientID']);
# print value # this will have all required IngredientID
答案 3 :(得分:0)
您每次通过该循环替换该值。您应该添加该值,而不是。
首先将值创建为空列表(在循环之前),然后在循环的每次迭代中,追加到该列表:
value = []
rec = recipes['Recipes'][0]['Ingredients']
for records in rec:
value.append({'Ingredient ID': records['IngredientID']})
然而,拥有一个字典列表,其中每个字典具有一个具有相同已知密钥的单个值,似乎有点无意义。根据您的要求,您可能想要这样做:
value.append(rec)
或
value.append(records['IngredientID'])
答案 4 :(得分:0)
您当前实施的一个问题是,每次找到新的value
时,ingredient ID
的值都会被覆盖。如果您需要获取值列表,那么即使添加新值,您也必须确保保留所有旧值。
如果您使values
列表然后使用追加,您将获得所需的所有值:
values = []
for records in rec:
ingredient_id = records.get('IngredientID', None)
if ingredient_id:
values.append(ingredient_id)
最好使用dict.get()
方法检索值,然后检查它是否存在;否则你可能会遇到例外情况。
答案 5 :(得分:0)
如果您只想要ID,可以使用列表推导,如下所示:
ids = [records['IngredientID'] for records in recipes['Recipes'][0]['Ingredients']]
答案 6 :(得分:0)
另一种方法,defaultdict
from collections import defaultdict
recipe_ids = defaultdict(list)
for records in rec:
recipe_ids['recipe id'].append(records.get('IngredientID'))
答案 7 :(得分:0)
如果您只想要IngredientID
的列表,请使用[rec['IngredientID'] for rec in records]
records = [{
"FullMeasure": "1/2 cup",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 1142,
"IngredientName": "frozen corn",
"IngredientStep": 10,
"LanguageId": 0,
"PostPreparation": ", thawed, drained",
"PrePreparation": "",
"QuantityNum": 0,
"QuantityText": "1/2",
"QuantityUnit": "cup",
"RecipeIngredientID": 6291555,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "1/4 cup",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 1523,
"IngredientName": "red pepper",
"IngredientStep": 20,
"LanguageId": 0,
"PostPreparation": "s",
"PrePreparation": "chopped",
"QuantityNum": 0,
"QuantityText": "1/4",
"QuantityUnit": "cup",
"RecipeIngredientID": 6291554,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "2 Tbsp.",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 20197,
"IngredientName": "chopped green chiles",
"IngredientStep": 30,
"LanguageId": 0,
"PostPreparation": ", drained",
"PrePreparation": "canned ",
"QuantityNum": 2,
"QuantityText": "2",
"QuantityUnit": "Tbsp.",
"RecipeIngredientID": 6291552,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 19682,
"IngredientName": "KRAFT DELI DELUXE Process American Cheese Slice",
"IngredientStep": 80,
"LanguageId": 0,
"PostPreparation": "s",
"PrePreparation": "",
"QuantityNum": 4,
"QuantityText": "4",
"QuantityUnit": "",
"RecipeIngredientID": 6291558,
"TrialMeasure": "",
"TrialWeight": ""
}
]
[rec['IngredientID'] for rec in records]
输出:
[1142, 1523, 20197, 19682]
答案 8 :(得分:0)
鉴于答案的数量,我认为嘿为什么不再一个。
如果您有复杂的表达式,您可能需要查看jmespath
,xslt
json
,例如:
>>> import jmespath
>>> jmespath.search('Recipes[0].Ingredients[*].IngredientID', recipes)
[1142, 1523, 20197, 19682]