Python Forum
Quickest way of splitting a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quickest way of splitting a string
#1
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
Reply
#2
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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using a function: splitting, joining, and slicing a string Drone4four 2 4,850 Dec-27-2018, 07:52 AM
Last Post: perfringo
  [split] Splitting a string into six pieces susmis666 1 2,218 Dec-25-2018, 06:16 PM
Last Post: ichabod801
  Splitting A String In Python Saif133 6 6,407 Jan-14-2017, 04:09 AM
Last Post: Saif133

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020