Question about regular expression

Joel Goldstick joel.goldstick at gmail.com
Wed Sep 30 15:21:43 EDT 2015


On Wed, Sep 30, 2015 at 2:50 PM, Emile van Sebille <emile at fenx.com> wrote:

> On 9/30/2015 11:34 AM, massi_srb at msn.com wrote:
>
>> Hi everyone,
>>
>> firstly the description of my problem. I have a string in the following
>> form:
>>
>> s = "name1 name2(1) name3 name4 (1, 4) name5(2) ..."
>>
>> 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:
>>
>> d = {
>>      "name1":(0, 0),
>>      "name2":(1, 0),
>>      "name3":(0, 0),
>>      "name4":(1, 4),
>>      "name5":(2, 0),
>> }
>>
>> I guess this problem can be tackled with regular expressions,
>>
>
> Stop there!  :)
>
> 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
>
> >>> "name1 name2(1) name3 name4(1,4) name5(2)".split()
> ['name1', 'name2(1)', 'name3', 'name4(1,4)', 'name5(2)']
>
> then create the dict from the result.


In your original string, splitting on spaces gives this:

>>> s = "name1 name2(1) name3 name4 (1, 4) name5(2)"
>>> s.split(' ')
['name1', 'name2(1)', 'name3', 'name4', '(1,', '4)', 'name5(2)']
>>>

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.

>>> s = "name1 name2(1) name3 name4(1,4) name5(2)"
>>> s.split(' ')
['name1', 'name2(1)', 'name3', 'name4(1,4)', 'name5(2)']

>>>

>From here writing the dictionary shouldn't be too hard.

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.

>
>
-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150930/d8319197/attachment.html>


More information about the Python-list mailing list