Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use this:</p> <pre><code># Get rid of unwanted part of the string text='idle (images=green:200, inpadoc=green:60, other=green:1000, retrieval=green:200, search=green:30)' text = text[text.find(' ')+1:].strip('()') # Define the converter function def converter(string): [key,value] = string.split('=') # Split into ['images','green:200'] value = value.split(':') # Split into ['images',['green','200']] value[1] = int(value[1]) # Convert the number into int ['images',['green':200]] return [key,value] print dict(map(converter,text.split(', '))) </code></pre> <p>So first, you get rid of the 'idle' and the parentheses.</p> <p>Then you define the function to convert each entry (<code>images=green:200</code>) into a two-element list (<code>['images',['green',200]]</code>), which will be converted into dictionary automatically using the <code>dict</code> function.</p> <p>Using <code>map</code> function is faster than just iterating over the list, which is the beauty of Python.</p> <p>EDIT:</p> <p>Oops, apparently using this <code>map</code> function is not faster than simply iterating over the list.</p> <pre><code>import time startTime = time.time() for i in range(100000): dict(map(converter,text.split(', '))) print 'Done in %.3fs' % (time.time()-startTime) # Done in 1.703s startTime = time.time() for i in range(100000): result = {} for element in text.split(', '): [key,value] = element.split('=') value = value.split(':') value[1] = int(value[1]) result[key] = value print 'Done in %.3fs' % (time.time()-startTime) # Done in 1.405s </code></pre> <p>But you have your options to select. =D</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload