[Tutor] convert to dict

Crabtree, Chad Chad.Crabtree at nationalcity.com
Tue Apr 27 11:59:10 EDT 2004


I know there is a more spiffy way to do this but I can never remember them
this is how I would do it.

IDLE 1.0.2      
>>> lb = ['TEMP01.DBF : 1024', 'TEMP02.DBF : 1024', 'TEMP03.DBF : 1024',
'TEMP04.DBF : 1024', 'TEMP05.DBF : 1024', 'TEMP06.DBF : 1024']
>>> def makeDict(adict):
	result={}
	for x in adict:
		key,value=x.split(':')
		result[key]=value
	return result

>>> makeDict(lb)
{'TEMP02.DBF ': ' 1024', 'TEMP01.DBF ': ' 1024', 'TEMP06.DBF ': ' 1024',
'TEMP05.DBF ': ' 1024', 'TEMP04.DBF ': ' 1024', 'TEMP03.DBF ': ' 1024'}
>>> 

If you need the values to be integers just int() them.



> -----Original Message-----
> From: Larry Blair [mailto:lbblair at adaptisinc.com]
> Sent: Tuesday, April 27, 2004 11:33 AM
> To: tutor at python.org
> Subject: [Tutor] convert to dict
> 
> 
> I have been doing Python for only about 2 weeks so sorry for 
> the beginners
> question. I have looked in the archives, etc. and suspect I don't know
> enough python vocabulary to ask the correct questions. I am 
> converting some
> Jython programs I inherited and I think there are built-ins to do this
> easier in Python.
> 
> I have a string (below) that I want to convert to a 
> dictionary. I think I
> can do it with a for loop and using split but just don't 
> quite have the
> skills to manipulate the data. I am getting the data from a 
> text properties
> file I read into the program.
> 
> Thanks for any help
> Larry
> 
> 
> lb = ['TEMP01.DBF : 1024', 'TEMP02.DBF : 1024', 'TEMP03.DBF : 1024',
> 'TEMP04.DBF : 1024', 'TEMP05.DBF : 1024', 'TEMP06.DBF : 1024']
> 
> 
> 
> 
> 
> 
> 
> __________________________________ 
> Confidentiality Notice: This e-mail message, including any 
> attachments, is
> for the sole use of the intended recipient(s) and may contain 
> information
> that is confidential privileged and/or exempt from disclosure under
> applicable law.  Any unauthorized review, use, disclosure or 
> distribution is
> prohibited.  If you are not the intended recipient, please contact the
> sender by reply e-mail and destroy all copies of the original message.
> Thank you.
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-------------------------------------------------------------------------------------------
***National City made the following annotations
-------------------------------------------------------------------------------------------

This communication is a confidential and proprietary business communication.
It is intended solely for the use of the designated recipient(s).  If this
communication is received in error, please contact the sender and delete this
communication.
===========================================================================================



More information about the Tutor mailing list