dictionaries, iteration code

Thread Solved

Join Date: Oct 2009
Posts: 7
Reputation: Mona1990 is an unknown quantity at this point 
Solved Threads: 0
Mona1990 Mona1990 is offline Offline
Newbie Poster

dictionaries, iteration code

 
0
  #1
Nov 21st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 243
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 70
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #2
Nov 21st, 2009
hi
here's a hint how to create a new dictionary for the car makers:

  1. cars = { "ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda" }
  2.  
  3. def build_car_maker( car ):
  4. d = {}
  5. # A loop through all the cars using the .values() method
  6. for each in car.values():
  7. d[ each ] = [] # Set an empty list as value to a car
  8. return d
  9.  
  10. 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
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: Mona1990 is an unknown quantity at this point 
Solved Threads: 0
Mona1990 Mona1990 is offline Offline
Newbie Poster
 
0
  #3
Nov 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 243
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 70
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #4
Nov 21st, 2009
right,

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

  1. cars = { "ABCD 123":"Mazda", "LFS 000":"Kia", "WINNR":"Ford", "XTRA":"Ford", "UR GR8":"Mazda" }
  2.  
  3. def build_car_maker( car ):
  4. d = {}
  5. # A loop through all the cars using the .values() method
  6. for each in car.values():
  7. d[ each ] = [] # Set an empty list as value to a car
  8. # This is from my previous post
  9.  
  10. # 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.
  11. for licence in car:
  12. # If the licence has the manufacturer as value, add the licence to the list of licences for the manufacturer.
  13. if car[ licence ] == each:
  14. d[ each ].append( licence )
  15. return d
  16.  
  17. print build_car_maker( cars )

Hope this makes it a bit clearer
Last edited by masterofpuppets; Nov 21st, 2009 at 6:12 pm. Reason: Forgot code tags AGAIN
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: Mona1990 is an unknown quantity at this point 
Solved Threads: 0
Mona1990 Mona1990 is offline Offline
Newbie Poster
 
0
  #5
Nov 22nd, 2009
Alright, that makes sense! Thanks a lot!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 402 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC