Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate string and number in Python list? [closed]

Tags:

python

I have a list as below.

list = ['perfect','data1', 'queryda873', 'server187', 'tuplip91', 'time']

How can I separate tokens and numbers in the list? I want something like:

list = ['perfect','data', '1', 'queryda','873', 'server','187', 'tulip','91', 'time']

Thank you.

like image 669
theteddyboy Avatar asked Dec 03 '13 19:12

theteddyboy


1 Answers

You can try using regular expressions (re). In particular, \d+|\D+:

>>> import re
>>> 
>>> L = ['perfect','data1', 'queryda873', 'server187', 'tuplip91', 'time']
>>> 
>>> [s for v in (re.findall(r'\d+|\D+', s) for s in L) for s in v]
['perfect', 'data', '1', 'queryda', '873', 'server', '187', 'tuplip', '91', 'time']

By the way, you shouldn't name your variables list, since that name is taken by a built-in function.

like image 107
arshajii Avatar answered Oct 21 '22 10:10

arshajii