Python Forum

Full Version: Quickest way of splitting a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a string with the following format: 'bbb 3 3', i.e a string and 2 numbers.
I need the 3 data, and i am using str.split() to get that result. The problem is it is a bit slow. Is there a quicker way to extract the same information, using regex or any other method?

Thank you very much for your help
I should not be slow,just splitting out numbers should take no time at all.
>>> s.split()
>>> s = 'bbb 3 3'
>>> s = s.split()
>>> s
['bbb', '3', '3']
>>> s[-2:]
['3', '3']
>>> # To integer
>>> [int(i) for i in s[-2:]]
[3, 3]
Can use regex to get number,but there should be no speed advantage to talk about.
>>> import re
>>> 
>>> s = 'bbb 3 3'
>>> re.findall(r'\d', s)
['3', '3']