python - Adding car make model year country to dictionary -
this function supposed loop through dictionary(database):
{ data_base = { "mercedes": [("e-class", 1970, "classic" , "germany"),("clk", 2000, "sport" , "poland")] "fiat": [("uno" , 1980, "coupe" , "italy")] "jaguar" : [("s-type", 2000, "classic", "england"),("x-type", 2005,"luxury", "england")] }
function accepts database (above example), updates other information it. if car in , attributes match, no duplication. also, sort asciibetically car model. function updates only, no returns.
functionx (data_base,make,model,year,style,country): key,value in data_base.items(): if key == make , value[0] != model: # condition ensure update not duplicate database[key].extend((model,year,style,country)):
when looping on data_base.items()
, value
not think is. list of models associated make of car. e.g. [("uno" , 1980, "coupe" , "italy")]
key 'fiat'
. when checking duplicate model, need iterate on list make sure model not found:
def functionx (data_base,make,model,year,style,country): key, value in data_base.items(): # first find models associated make present_models = set(data[0] data in value) if key == make , model not in present_models: # condition ensure update not duplicate database[key].extend((model,year,style,country))
next point: you're not taking advantage of fact database dictionary. there's no need loop through key-value pairs when know key we're interested in. can following:
def functionx (data_base,make,model,year,style,country): if make in data_base: present_models = set(data[0] data in data_base[make]) if model not in present_models: database[make].extend((model,year,style,country)) else: # make not found can add info without worrying duplication data_base[make] = [(model,year,style,country)]
set(data[0] data in data_base[make])
creates set of model names under make. can think of set list containing no duplicates. provides fast checking whether item in set (whereas list have check each item one-by-one). syntax creating set uses list-comprehension in python can read here: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions.
finally, sorting can done pretty using sort
. each list of models contains tuples , tuples sorted lexicographically (the first items compared, if they're equal, second items compared , on...) default in python. want list sorted first items in our tuples (the model) can call sort
on list after adding new item. in general, can specify predicate use when sorting setting key parameter on sort
(e.g. sort(my_list, key=<some function>)
).
def functionx (data_base,make,model,year,style,country): if make in data_base: present_models = set(data[0] data in data_base[make]) if model not in present_models: database[make].append((model,year,style,country)) sort(database[make]) else: # make not found can add info without worrying duplication data_base[make] = [(model,year,style,country)]
i changed extend
append
since adding single item.
this solution made more efficient if utilize fact lists of models sorted. fact can perform binary searches when checking if model present , when inserting new model.
Comments
Post a Comment