Question about regular expression
Denis McMahon
denismfmcmahon at gmail.com
Fri Oct 2 14:25:34 EDT 2015
On Wed, 30 Sep 2015 23:30:47 +0000, Denis McMahon wrote:
> 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)}
Oh yeah, the code:
#!/usr/bin/python
import re
s = 'fred jim(1) alice tom (1, 4) peter (2) andrew(3,4) janet( 7,6 ) james
( 7 ) mike ( 9 ) jon ( 6 , 3 ) charles(0,12)'
bits = s.replace('(', ' ').replace(',', ' ').replace(')', ' ').split(' ')
d = {}
namep = re.compile('^[A-Za-z]+$')
numbp = re.compile('^[0-9]+$')
for bit in bits:
if namep.match(bit):
d[bit] = [0,0]
w = bit
nums = 0
if numbp.match(bit):
n = int(bit)
d[w][nums] = n
nums += 1
d = {x:tuple(d[x]) for x in d}
print s
print d
It uses regex to determine if the list element being processed is a name
or a number, which makes for 2 very simple patterns.
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list