943,070 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 645
  • Python RSS
Nov 21st, 2009
0

dictionaries, iteration code

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mona1990 is offline Offline
7 posts
since Oct 2009
Nov 21st, 2009
0
Re: dictionaries, iteration code
hi
here's a hint how to create a new dictionary for the car makers:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 21st, 2009
0
Re: dictionaries, iteration code
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mona1990 is offline Offline
7 posts
since Oct 2009
Nov 21st, 2009
0
Re: dictionaries, iteration code
right,

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

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 22nd, 2009
0
Re: dictionaries, iteration code
Alright, that makes sense! Thanks a lot!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mona1990 is offline Offline
7 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: sudoku solver
Next Thread in Python Forum Timeline: Dictionaries Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC