typecasting: a string object to a dictionary object

Peter Abel PeterAbel at gmx.net
Sat Mar 13 10:33:25 EST 2004


dont bother <dontbotherworld at yahoo.com> wrote in message news:<mailman.344.1079154269.19534.python-list at python.org>...
> Hi,
> Thanks! Just one more favour.
> I want to sort the entries on this new dictionary.
> (Increasing order) and convert it back to a string
> where each values are not separated by , but by space.
> 
> ie. for example: I have a string s of the form
> index: value, index: value, index: value
> 
> I converted it to a dictionary by the method you
> suggested. I want to sort the entries now, with
> increasing order of index.
> 
> Once I have the sorted dictionary, I want to convert
> it back to a string of the form
> 
> 1 index:value index:value index:value
> 
> 
> Note, that 1 is added because I am creating feature
> vectors of the type "spam". index: value pairs are now
> separated by space instead of comma and finally I want
> to write this to a file.
> 
> The whole notion of making a string to a dictionary
> was because of sorting.
> 
> Can you extend this piece of code for the above
> objective. I will indeed be very grateful to you.
> 
> 
> import string
> 
> # define a test string
> s1 = "<name1: value1, name2: value2, name3: value3>"
> # get rid of the < and > by taking a slice
> s1 = s1[1:-1]
> # split string at the commas
> s2 = string.split(s1,',')
> mydict = {}
> for item in s2:
>         a,b = string.split(item,":")
>         mydict[a] = b
>         print mydict[a]
> print "Dictionary is: ", mydict
> 
.
[snip]
.

Here is another approch:

>>> import string
>>> # define a test_string
>>> test_string='<item1:nothing, myitem:good, anotheritem  :
with_blanks   ,   and_so_on:empty       >'
>>> # define a function that strips off blanks
>>> strip_off=string.strip
>>> # define a function that splits at ":" and strips the resulting
strings
>>> split_colon=lambda s:map(strip_off,string.split(s,':'))
>>> # define a function the splits at ",", splits the result at ":"
and strips it
>>> split_komma=lambda s:map(split_colon,string.split(s,','))
>>> # Now apply your function on the test_string without "<" and ">"
>>> result=split_komma(test_string[1:-1])
>>> # Sort your result
>>> result.sort()
>>> # That's what you want
>>> result
[['and_so_on', 'empty'], ['anotheritem', 'with_blanks'], ['item1',
'nothing'], ['myitem', 'good']]
>>> # Create a dictionary of the result
>>> aDictionary=dict(result)
>>> aDictionary
{'myitem': 'good', 'item1': 'nothing', 'and_so_on': 'empty',
'anotheritem': 'with_blanks'}
>>> # Write your result in one line separated with blanks
>>> # First create formatted strings for "key:value"-pairs
>>> result_string_lines=map(lambda (k,v):'%s:%s' %(k,v),result)
>>> result_string_lines
['and_so_on:empty', 'anotheritem:with_blanks', 'item1:nothing',
'myitem:good']
>>> # Now print it in one line, separated with blanks
>>> print 1,' '.join(result_string_lines)
1 and_so_on:empty anotheritem:with_blanks item1:nothing myitem:good
>>> 

Hope this helps
Regards
Peter



More information about the Python-list mailing list