typecasting: a string object to a dictionary object

Byron Morgan bmorgan at usa.net
Sat Mar 13 01:53:13 EST 2004


Here is one way:

a = {}
feature_vector = '1:x 2:y 3:z'
for each in feature_vector.split():
    a[each.split(':')[0]]=each.split(':')[1]


Byron Morgan


dont bother <dontbotherworld at yahoo.com> wrote in message news:<mailman.340.1079150274.19534.python-list at python.org>...
> Here am I,
> On the border...if someone can help me, it would be
> nice:
> 
> I have a string:
> 
> feature_vector. It is of the form 
> <index: value, index: value, index: value>
> 
> I want to make this string into a dictionary so that I
> can apply .keys() method
> 
> If I apply .keys() method straight away I get this
> error:
> 
> AttributeError: 'str' object has no attribute 'keys'
> 
> Is there a way in which I can convert/typecast my
> string thing to dictionary ??
> 
> Thax in advance
> Dont
> 
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail - More reliable, more storage, less spam
> http://mail.yahoo.com



More information about the Python-list mailing list