I'm having difficulty in using a dictionary to classify speed of objects.
Input:
Object_dict={"Airbus 380":{"Country":"France,Germany,Spain,UK","Top Speed(Mach)":0.89},"Concorde":{"Country":"France,UK","Top Speed(Mach)":2.01}, "Boeing X-43":{"Country": "USA","Top Speed(Mach)":9.6}}
Ouput:
Objects_by_Mach={"Subsonic":["Airbus 380"],"Transonic":[],"Supersonic":["Concorde"],"Hypersonic":["Boeing X-43"]}
This is my code:
Mach_scale = {"Subsonic": 0,
"Transonic": 1,
"Supersonic":5,
"Hypersonic":5 ,
}
#Subsonic object has speed of Mach<0
#Transsonic object has speed of Mach=1
#Supersonic object has speed of 1<Mach<5
#Hypersonic object has speed of Mach>5
def mach_speeds(dict1):
Objects_by_Mach={}
for object,data in dict1.items():
for value in data["Top Speed(Mach)"]:
Subsonic=[object for object in dict1 if value<=Mach_scale["Transonic"] and value>Mach_scale["Subsonic"] in dict1["Top Speed(Mach)"] in dict1.values()]
Transonic=[object for object in dict1 if value==Mach_scale["Transonic"] in Mach_scale["Top Speed(Mach)"] in dict1.values()]
Supersonic=[object for object in dict1 if value<=Mach_scale["Supersonic"] and value>Mach_scale["Transonic"] in dict1["Top Speed(Mach)"] in dict1.values()]
Hypersonic=[object for object in dict1 if value>Mach_scale["Hypersonic"] in dict1["Top Speed(Mach)"] in dict1.values()]
return Objects_by_Mach.update({"Subsonic":Subsonic,"Transonic":Transonic,"Supersonic":Supersonic,"Hypersonic":Hypersonic})
print(mach_speeds(Object_dict))
Thanks in advance again fellow SO'ers.
CodePudding user response:
You can generalise and therefore shorten your code by specifying ranges for the Mach scale. The values used here may not be correct but can be easily adjusted to suit.
Mach_scale = {"Subsonic": (0.0, 0.8),
"Transonic": (0.8, 1.2),
"Supersonic": (1.2, 5.0),
"Hypersonic": (5.0, 10.0),
"High-hypersonic": (10.0, float('inf'))
}
Object_dict = {"Airbus 380": {"Country": "France,Germany,Spain,UK", "Top Speed(Mach)": 0.89},
"Concorde": {"Country": "France,UK", "Top Speed(Mach)": 2.01},
"Boeing X-43": {"Country": "USA", "Top Speed(Mach)": 9.6}}
result = dict()
def getmach(m):
for k, v in Mach_scale.items():
if m >= v[0] and m < v[1]:
return k
for k, v in Object_dict.items():
result.setdefault(getmach(v['Top Speed(Mach)']), []).append(k)
print(result)
Output:
{'Subsonic': ['Airbus 380'], 'Transonic': ['Concorde'], 'Hypersonic': ['Boeing X-43']}
CodePudding user response:
First of all please note that this question is very specific and will most likely help only you, We love questions that are general and will help as many people as possible!
There are some things in your code that are considered bad practice and are problematic.
- First looks like the indentations are not correct
- Second, dont use the reserved word object - use a different variable name
- Notice that in each iteration you are creating a new list, looks to me that you want to update it - not creating a new one.
I would try something like the following code:
input = {
"Airbus 380":{"Country":"France,Germany,Spain,UK","Top Speed(Mach)":0.89},
"Concorde":{"Country":"France,UK","Top Speed(Mach)":2.01},
"Boeing X-43":{"Country": "USA","Top Speed(Mach)":9.6}
}
mach_scale = {"Subsonic": 0.8,
"Transonic": 1.2,
"Supersonic":5,
"Hypersonic":5
}
"""
subsonic speed - below 0.8 mach
transonic speed - between 0.8 - 1.2 mach
supersonic speed - between 1.2 - 5 mach
hypersonic speed - above 5 mach
"""
def mach_speeds(airplane_data):
subsonic, transonic, supersonic, hypersonic = [], [], [], []
for plane, data in airplane_data.items():
top_speed = data["Top Speed(Mach)"]
if top_speed <= mach_scale["Subsonic"]:
subsonic.append(plane)
elif top_speed <= mach_scale["Transonic"]:
transonic.append(plane)
elif top_speed <= mach_scale["Supersonic"]:
supersonic.append(plane)
else:
hypersonic.append(plane)
result = {}
result["Subsonic"] = subsonic
result["Transonic"] = transonic
result["Supersonic"] = supersonic
result["Hypersonic"] = hypersonic
return result
if __name__ == "__main__":
print(mach_speeds(input))
Output:
{'Subsonic': [], 'Transonic': ['Airbus 380'], 'Supersonic': ['Concorde'], 'Hypersonic': ['Boeing X-43']}