Question about regular expression

Denis McMahon denismfmcmahon at gmail.com
Wed Sep 30 19:30:47 EDT 2015


On Wed, 30 Sep 2015 11:34:04 -0700, massi_srb wrote:

> firstly the description of my problem. I have a string in the following
> form: .....

The way I solved this was to:

1) replace all the punctuation in the string with spaces

2) split the string on space

3) process each thing in the list to test if it was a number or word

4a) add words to the dictionary as keys with value of a default list, or
4b) add numbers to the dictionary in the list at the appropriate position

5) convert the list values of the dictionary to tuples

It seems to work on my test case:

s = "fred jim(1) alice tom (1, 4) peter (2) andrew(3,4) janet( 7,6 ) james
( 7 ) mike ( 9 )"

d = {'mike': (9, 0), 'janet': (7, 6), 'james': (7, 0), 'jim': (1, 0), 
'andrew': (3, 4), 'alice': (0, 0), 'tom': (1, 4), 'peter': (2, 0), 'fred': 
(0, 0)}




-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list