hi all,
I'm trying to understand regex for the first time, and it would be very
helpful to get an example. I have an old(er) script with the following
task - takes a string I copy-pasted and wich always has the same format:
[color=blue][color=green][color=darkred]
>>> print stuff[/color][/color][/color]
Yellow hat 2 Blue shirt 1
White socks 4 Green pants 1
Blue bag 4 Nice perfume 3
Wrist watch 7 Mobile phone 4
Wireless cord! 2 Building tools 3
One for the money 7 Two for the show 4
[color=blue][color=green][color=darkred]
>>> stuff[/color][/color][/color]
'Yellow hat\t2\tBlue shirt\t1\nWhite socks\t4\tGreen pants\t1\nBlue
bag\t4\tNice perfume\t3\nWri st watch\t7\tMobil e phone\t4\nWirel ess
cord!\t2\tBuild ing tools\t3\nOne for the money\t7\tTwo for the show\t4'
I want to put items from stuff into a dict like this:[color=blue][color=green][color=darkred]
>>> print mydict[/color][/color][/color]
{'Wireless cord!': 2, 'Green pants': 1, 'Blue shirt': 1, 'White socks':
4, 'Mobile phone': 4, 'Two for the show': 4, 'One for the money': 7,
'Blue bag': 4, 'Wrist watch': 7, 'Nice perfume': 3, 'Yellow hat': 2,
'Building tools': 3}
Here's how I did it:[color=blue][color=green][color=darkred]
>>> def putindict(items ):[/color][/color][/color]
.... items = items.replace(' \n', '\t')
.... items = items.split('\t ')
.... d = {}
.... for x in xrange( len(items) ):
.... if not items[x].isdigit(): d[items[x]] = int(items[x+1])
.... return d[color=blue][color=green][color=darkred]
>>>
>>> mydict = putindict(stuff )[/color][/color][/color]
I was wondering is there a better way to do it using re module?
perheps even avoiding this for loop?
thanks!
I'm trying to understand regex for the first time, and it would be very
helpful to get an example. I have an old(er) script with the following
task - takes a string I copy-pasted and wich always has the same format:
[color=blue][color=green][color=darkred]
>>> print stuff[/color][/color][/color]
Yellow hat 2 Blue shirt 1
White socks 4 Green pants 1
Blue bag 4 Nice perfume 3
Wrist watch 7 Mobile phone 4
Wireless cord! 2 Building tools 3
One for the money 7 Two for the show 4
[color=blue][color=green][color=darkred]
>>> stuff[/color][/color][/color]
'Yellow hat\t2\tBlue shirt\t1\nWhite socks\t4\tGreen pants\t1\nBlue
bag\t4\tNice perfume\t3\nWri st watch\t7\tMobil e phone\t4\nWirel ess
cord!\t2\tBuild ing tools\t3\nOne for the money\t7\tTwo for the show\t4'
I want to put items from stuff into a dict like this:[color=blue][color=green][color=darkred]
>>> print mydict[/color][/color][/color]
{'Wireless cord!': 2, 'Green pants': 1, 'Blue shirt': 1, 'White socks':
4, 'Mobile phone': 4, 'Two for the show': 4, 'One for the money': 7,
'Blue bag': 4, 'Wrist watch': 7, 'Nice perfume': 3, 'Yellow hat': 2,
'Building tools': 3}
Here's how I did it:[color=blue][color=green][color=darkred]
>>> def putindict(items ):[/color][/color][/color]
.... items = items.replace(' \n', '\t')
.... items = items.split('\t ')
.... d = {}
.... for x in xrange( len(items) ):
.... if not items[x].isdigit(): d[items[x]] = int(items[x+1])
.... return d[color=blue][color=green][color=darkred]
>>>
>>> mydict = putindict(stuff )[/color][/color][/color]
I was wondering is there a better way to do it using re module?
perheps even avoiding this for loop?
thanks!
Comment