用于对列表中对象的速度进行分类的字典
Dictionary to classify speed of objects in a list
我在使用字典对物体速度进行分类时遇到困难。
输入:
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}}
输出:
Objects_by_Mach={"Subsonic":["Airbus 380"],"Transonic":[],"Supersonic":["Concorde"],"Hypersonic":["Boeing X-43"]}
这是我的代码:
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))
再次提前感谢各位 SO'ers。
您可以通过指定马赫标度的范围来概括并因此缩短您的代码。此处使用的值可能不正确,但可以轻松调整以适应。
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)
输出:
{'Subsonic': ['Airbus 380'], 'Transonic': ['Concorde'], 'Hypersonic': ['Boeing X-43']}
首先请注意,这个问题非常具体,很可能只会帮助您,我们喜欢笼统的问题,并且会帮助尽可能多的人!
您的代码中有些东西被认为是不好的做法并且存在问题。
- 首先看起来缩进不正确
- 其次,不要使用保留字对象——使用不同的变量名
- 请注意,在我看来,在每次迭代中您都在创建一个新列表
您想要更新它 - 而不是创建一个新的。
我会尝试类似下面的代码:
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))
输出:
{'Subsonic': [], 'Transonic': ['Airbus 380'], 'Supersonic': ['Concorde'], 'Hypersonic': ['Boeing X-43']}
我在使用字典对物体速度进行分类时遇到困难。
输入:
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}}
输出:
Objects_by_Mach={"Subsonic":["Airbus 380"],"Transonic":[],"Supersonic":["Concorde"],"Hypersonic":["Boeing X-43"]}
这是我的代码:
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))
再次提前感谢各位 SO'ers。
您可以通过指定马赫标度的范围来概括并因此缩短您的代码。此处使用的值可能不正确,但可以轻松调整以适应。
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)
输出:
{'Subsonic': ['Airbus 380'], 'Transonic': ['Concorde'], 'Hypersonic': ['Boeing X-43']}
首先请注意,这个问题非常具体,很可能只会帮助您,我们喜欢笼统的问题,并且会帮助尽可能多的人!
您的代码中有些东西被认为是不好的做法并且存在问题。
- 首先看起来缩进不正确
- 其次,不要使用保留字对象——使用不同的变量名
- 请注意,在我看来,在每次迭代中您都在创建一个新列表 您想要更新它 - 而不是创建一个新的。
我会尝试类似下面的代码:
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))
输出:
{'Subsonic': [], 'Transonic': ['Airbus 380'], 'Supersonic': ['Concorde'], 'Hypersonic': ['Boeing X-43']}