Python 2.7: Faster/better way to extract all integer values from a string? -
i want able extract integer numbers user-defined string includes integers, floats, , strings. using split() , isdigit() methods yank out integers , concatenate them list:
stringy = "if y0u can 66.6 r34d 25 this, you're 29 t00 close." stringz = [item item in stringy.split() if item.isdigit()] print stringz >> ['25','29']
this works, , it's fine need particular problem, got me thinking; seems me if string long, might not efficient way handle problem. there better way?
using regular expression work:
import re integers = re.compile(r'(?<!\s)\d+(?!\s)') integers.findall(stringy)
demo:
>>> import re >>> stringy = "if y0u can 66.6 r34d 25 this, you're 29 t00 close." >>> integers = re.compile(r'(?<!\s)\d+(?!\s)') >>> integers.findall(stringy) ['25', '29']
for shorter input example, .split()
, .isdigit()
list comprehension still faster though:
>>> import timeit >>> timeit.timeit('[item item in stringy.split() if item.isdigit()]', 'from __main__ import stringy') 3.1379640102386475 >>> timeit.timeit('integers.findall(stringy)', 'from __main__ import stringy, integers') 4.119225978851318
it improves little longer inputs:
>>> stringy = ''.join([stringy _ in range(100)]) >>> timeit.timeit('[item item in stringy.split() if item.isdigit()]', 'from __main__ import stringy', number=10000) 2.5325310230255127 >>> timeit.timeit('integers.findall(stringy)', 'from __main__ import stringy, integers', number=10000) 4.044284105300903
Comments
Post a Comment