Amazon Last Mile Dataset
An example of how we use a Python dictionary in a real-world scenario.
Source: https://registry.opendata.aws/amazon-last-mile-challenges/
Challenge:
find the volume of one package in the dataset
Let's explore the dataset
# Python program to read json file
import json
# Opening JSON file
f = open('new_package_data.json')
# returns JSON object as a dictionary
data = json.load(f)
# Closing file
f.close()
Run to view results
type(data)
Run to view results
list(data)
Run to view results
print(data)
Run to view results
data
Run to view results
data['RouteID_15baae2d-bf07-4967-956a-173d4036613f']
Run to view results
data['RouteID_15baae2d-bf07-4967-956a-173d4036613f']['AH']['PackageID_07017709-2ddd-4c6a-8b7e-ebde70a4f0fa']['time_window']
Run to view results
dimension_dict = data['RouteID_15baae2d-bf07-4967-956a-173d4036613f']['AH']['PackageID_07017709-2ddd-4c6a-8b7e-ebde70a4f0fa']['dimensions']
print(dimension_dict)
Run to view results
AH_volume = dimension_dict['depth_cm'] * dimension_dict['height_cm'] * dimension_dict['width_cm']
AH_volume = round(AH_volume,2 )
Run to view results
print(f" The volume of AH is {AH_volume}")
Run to view results
data.items()
Run to view results
for a, b in data.items():
for c,d in b.items():
for e,f in d.items():
print(f)
Run to view results
volume_dict = {}
# Loop through the outer dictionary
for route_id, destinations in data.items():
# Loop through each destination in the route
for destination_code, packages in destinations.items():
# Loop through each package in the destination
for package_id, package_details in packages.items():
# Access the dimensions of the package
dimensions = package_details.get('dimensions', {})
depth = dimensions.get('depth_cm', 0)
height = dimensions.get('height_cm', 0)
width = dimensions.get('width_cm', 0)
# Calculate the volume of the package
volume = depth * height * width
# Store the volume in the dictionary using the package ID as the key
volume_dict[package_id] = volume
# Print the volume of each package
for package_id, volume in volume_dict.items():
print(f"Package ID: {package_id}, Volume: {volume} cm^3")
Run to view results
Run to view results