问题描述
我有一个相对简单的用例,我需要加载符合特定标准的车辆类型。此标准显示在“sample_input”变量中。
现在我需要展示符合正确标准的汽车。我正在读取我的 car
数据库 JSON,然后创建汽车对象并将它们添加到我的 carDatabase 列表中,我需要从该列表中选择适合用户请求的汽车。
所以基本上发生的事情是我在 Main.Py 中加载了一个简单的 sample_input
变量
代码应该这样做,但它没有(总是欢迎更好的实现)
检查Name
是否为空[]
;如果是,继续前进;如果不是,则搜索具有相同名称的汽车,移至下一个标准,即原产国,检查它是否为空,如果不是,则查找具有名称和原产国的汽车,以及其他标准等。
import json
from Vehicle import Vehicle
sample_input = {
"Name": [],"country of origin": ["france","uk"],"transmission": [],"body type": ["coupe"],"drive type": [],"doors": [],"fuel type": []
}
def test():
with open("data.json") as omapper:
data = json.load(omapper)
Database= []
for i in data["vehicles"]:
name = i["Name"]
origin = i["country of origin"]
transmission = i["transmission"]
bodytype = i["body type"]
drivetype = i["drive type"]
doors = i["doors"]
fueltype = i["fuel type"]
vehicle= Vehicle(name,origin,transmission,bodytype,drivetype,doors,fueltype)
Database.append(car)
UserOutput = []
for vehicles in Database:
if vehicles.origin in sample_input["country of origin"] and vehicles.body_type in sample_input["body type"] and vehicles.doors in sample_input["doors"] != "":
print(vehicle.name)
if __name__ == "__main__":
test()
Vehicle.py
:模型类
class Vehicle:
def __init__(self,name,body_type,drive_type,fuel_type):
self.name = name
self.origin = origin
self.transmission = transmission
self.body_type = body_type
self.drive_type = drive_type
self.doors = doors
self.fuel_type = fuel_type
车辆 JSON:我的应用程序支持的所有车辆。
{
"attributes": {
"country of origin": ["japan","america","germany","south korea","italy","sweden"],"transmission": ["automatic","manual"],"body type": ["hatchback","sedan","SUV","ute","coupe","convertible","van"],"drive type": ["RWD","FWD","4WD"],"doors": ["4 door","2 door"],"fuel type": ["petrol","diesel","electric","hybrid"]
},"vehicles": [
{
"Name": "studebaker dictator","country of origin": "america","transmission": "manual","body type": "sedan","drive type": "RWD","doors": "2 door","fuel type": "petrol"
},{
"Name": "mitsubishi zero","country of origin": "japan","body type": "hatchback","drive type": "FWD",...
]
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)