| | |
dictionaries, iteration code
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
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!
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!
0
#2 Nov 21st, 2009
hi
here's a hint how to create a new dictionary for the car makers:
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
here's a hint how to create a new dictionary for the car makers:
Python Syntax (Toggle Plain Text)
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
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
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
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
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
Hope this makes it a bit clearer
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)
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
Last edited by masterofpuppets; Nov 21st, 2009 at 6:12 pm. Reason: Forgot code tags AGAIN
![]() |
Similar Threads
- Time complexity of algorithm (Computer Science)
- Code Snippet: Bouncing ball (C#)
- how to create each file for each iteration using loop (Java)
- cURL problems (PHP)
- Merging dictionaries (Python)
- Image Load: MIA (Python)
- XTEA Encryption (C++)
- while loop (C)
- Dictionary Keys (Python)
Other Threads in the Python Forum
- Previous Thread: sudoku solver
- Next Thread: Dictionaries Help
Views: 402 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
application beginner change character class client cmd code cx-freeze data database decimals development dictionary dynamic error examples excel exe file float format ftp function generator gui hack homework http import input java leftmouse library line linux list lists logging loop module mouse mysql mysqldb newb number numbers output parsing path pi port prime program programming projects push py2exe pygame pygtk pyqt python random recursion recursive redirect reverse schedule screensaverloopinactive script scrolledtext server sqlite ssh stdout string strings sudokusolver table tar terminal text thread threading time tkinter tlapse transparency tuple tutorial ubuntu unicode update urllib urllib2 variable web wikipedia windows wxpython





