Skip to content

Instantly share code, notes, and snippets.

@m0xb
Last active October 3, 2017 08:09
Show Gist options
  • Select an option

  • Save m0xb/045ae6e510893a225f623a505ec0c24b to your computer and use it in GitHub Desktop.

Select an option

Save m0xb/045ae6e510893a225f623a505ec0c24b to your computer and use it in GitHub Desktop.
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
]
@WorryingWonton
Copy link

WorryingWonton commented Oct 3, 2017

#Task 1.1
task1_1 = [donut['id'] for donut in donutjson if donut['name'] == 'Raised'][0]
print(task1_1)
#Task 2.1
task2_1 = [donut['ppu'] for donut in donutjson if donut['name'] == 'Old Fashioned'][0]
print(task2_1)
#Task 3.1
print(len([donut['topping'] for donut in donutjson if donut['id'] == '0003'][0]))
#Task 4.1
print(len([donut['batters']['batter'] for donut in donutjson if donut['id'] == '0001'][0]))
#Task 5.1
print([[topping['type'] for topping in donut['topping'] if topping['id'] == '5003'] for donut in donutjson][0][0])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment