<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 30, 2015 at 2:50 PM, Emile van Sebille <span dir="ltr"><<a href="mailto:emile@fenx.com" target="_blank">emile@fenx.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">On 9/30/2015 11:34 AM, <a href="mailto:massi_srb@msn.com" target="_blank">massi_srb@msn.com</a> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Hi everyone,<br>
<br>
firstly the description of my problem. I have a string in the following form:<br>
<br>
s = "name1 name2(1) name3 name4 (1, 4) name5(2) ..."<br>
<br>
that is a string made up of groups in the form 'name' (letters only) plus possibly a tuple containing 1 or 2 integer values. Blanks can be placed between names and tuples or not, but they surely are placed beween two groups. I would like to process this string in order to get a dictionary like this:<br>
<br>
d = {<br>
     "name1":(0, 0),<br>
     "name2":(1, 0),<br>
     "name3":(0, 0),<br>
     "name4":(1, 4),<br>
     "name5":(2, 0),<br>
}<br>
<br>
I guess this problem can be tackled with regular expressions,<br>
</blockquote>
<br></span>
Stop there!  :)<br>
<br>
I'd use string functions.  If you can control the string output to drop the spaces and always output in namex(a,b)<space>namey(c,d)... format, try starting with<br>
<br>
>>> "name1 name2(1) name3 name4(1,4) name5(2)".split()<br>
['name1', 'name2(1)', 'name3', 'name4(1,4)', 'name5(2)']<br>
<br>
then create the dict from the result.</blockquote><div><br></div><div>In your original string, splitting on spaces gives this:</div><div><br></div><div>>>> s = "name1 name2(1) name3 name4 (1, 4) name5(2)"</div><div>>>> s.split(' ')</div><div>['name1', 'name2(1)', 'name3', 'name4', '(1,', '4)', 'name5(2)']</div><div>>>> </div><div><br></div><div>That might be useful to start.  The space between the namex and (1,...) is problematic.  If your data could be that way, perhaps, preprocess the string to remove space before '(', and the space between commas.</div><div><div><br></div><div>>>> s = "name1 name2(1) name3 name4(1,4) name5(2)"</div><div>>>> s.split(' ')</div><div>['name1', 'name2(1)', 'name3', 'name4(1,4)', 'name5(2)']</div><div><br></div><div>>>> </div></div><br class=""><div>From here writing the dictionary shouldn't be too hard. </div><div><br></div><div>I'm sure there is a better way as this does a couple of loops, but once you get it working, you can optimize if necessary.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br></blockquote></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Joel Goldstick<br></div><a href="http://joelgoldstick.com" target="_blank">http://joelgoldstick.com</a>/stats/birthdays<br></div></div>
</div></div>