Hi!
I was wondering if someone could help with writing the code for the following function. I am having trouble with dictionaries and would greatly appreciate some hints!

thanks!


Write a function build_car_maker, which consumes a car registry dictionary, and produces a car maker dictionary, leaving the original dictionary unchanged. The car registry dictionary stores key:value pairs of the form licence_plate:car_maker, where both the key and the value are strings, for example "ABC 123":"Ford".

The new dictionary will be produced from the consumed dictionary, and its key:value pairs will be of the form
car_maker:[licence_plate, licence_plate, ...], i.e. the value associated with the car_maker name is the list of licence plates keys in the original dictionary which have that car_maker as their values.
While the dictionaries themselves are unsorted, the produced dictionary should have the list
of licence plate values stored in alphabetical order (you can use a built-in sorting method to
do this, or write you own helper function).

For example, suppose you have the car registry dictionary cars:
{"ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda"}
and you call build_car_maker(car), then the produced dictionary should be:
{"Kia":["LFS 000"], "Ford":["WINNR", "XTRA"], "Mazda":["ABCD 123", "UR GR8"]}

thanks a lot for any help! :)

Recommended Answers

All 4 Replies

Member Avatar for masterofpuppets

hi
here's a hint how to create a new dictionary for the car makers:

cars = { "ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda" }

def build_car_maker( car ):
    d = {}
    # A loop through all the cars using the .values() method
    for each in car.values():
        d[ each ] = [] # Set an empty list as value to a car
    return d

print build_car_maker( cars )

see if you could finish the function be adding a couple of lines to check the licence_plate :)

let me know if you need any help :)

P.S you could check my site. I've added some tutorials on dictionaries and other stuff. Here's the link http://8masterofpuppets8.webs.com/tutorials.htm

Alright!
Thank you for your help!

However, I do not get how to pick out the plate numbers for each if the values separately. Here, is What I tried so far,

cars = { "ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda" }

def build_car_maker( car ):
  d = {} # A loop through all the cars using the .values() method

  for f in car.keys():
        m [f] = sorted(car.keys(), key=lambda(v,k): (v,k))

  for each in car.values():
        d[ each ] = [m] # Set an empty list as value to a car


  return d

I also looked at your website for the dictionary tutorial, and I think its similar to the sorting you used for the letter count, but cant see m to apply it to my code.
If you can offer any more help , I would really appreciate it!

thanks :)

Member Avatar for masterofpuppets

right,

here's my version along with the licence stuff added. I'll try to explain what I'm doing with comments :)

cars = { "ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda" }
 
def build_car_maker( car ):
    d = {}
    # A loop through all the cars using the .values() method
    for each in car.values():
        d[ each ] = [] # Set an empty list as value to a car
        # This is from my previous post

        # Here you need to check each licence code, i.e. every key in the cars or car( in the function ) dictionary. Here 'each' is a car manufacturer.
        for licence in car:
            # If the licence has the manufacturer as value, add the licence to the list of licences for the manufacturer.
            if car[ licence ] == each:
                d[ each ].append( licence )
    return d
 
print build_car_maker( cars )

Hope this makes it a bit clearer :)

Alright, that makes sense! Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.