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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With