Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, April 15, 2016

Cleanest way to obtain the numeric prefix of a string

Cleanest way to obtain the numeric prefix of a string


What is the cleanest way to obtain the numeric prefix of a string in Python?

By "clean" I mean simple, short, readable. I couldn't care less about performance, and I suppose that it is hardly measurable in Python anyway.

For example:

Given the string '123abc456def', what is the cleanest way to obtain the string '123'?

The code below obtains '123456':

input = '123abc456def'  output = ''.join(c for c in input if c in '0123456789')  

So I am basically looking for some way to replace the if with a while.

Answer by zondo for Cleanest way to obtain the numeric prefix of a string


Here is my way:

output = input[:next((i for i,v in enumerate(input) if not v.isdigit()),None)]  

Answer by Kasramvd for Cleanest way to obtain the numeric prefix of a string


You can use itertools.takewhile which will iterate over your string (the iterable argument) until it encounters the first item which returns False (by passing to predictor function):

>>> from itertools import takewhile  >>> input = '123abc456def'  >>> ''.join(takewhile(str.isdigit, input))  '123'  

Answer by MSeifert for Cleanest way to obtain the numeric prefix of a string


One way, but not very efficient since it works through the whole string without break would be:

input_string = '123abc456def'  [input_string[:c] for c in range(len(input_string)) if input_string[:c].isdigit()][-1]  

This appends each substring with increasing size if it is a digit and then appends it. So the last element is the one you look for. Because it is the longest startstring that is still a digit.

Answer by Tal Avissar for Cleanest way to obtain the numeric prefix of a string


This is the simplest way to extract a list of numbers from a string:

>>> import re  >>> input = '123abc456def'  >>> re.findall('\d+', s)  ['123','456']  

If you need a list of int's then you might use the following code:

   >>> map(int, re.findall('\d+', input ))     [123,456]  

And now you can access the first element [0] from the above list

Answer by Mr. E for Cleanest way to obtain the numeric prefix of a string


You could use regex

import re  initialNumber = re.match(r'(?P\d+)', yourInput).group('number')  

Answer by demented hedgehog for Cleanest way to obtain the numeric prefix of a string


input[:len(input) - len(input.lstrip("0123456789"))]  

Answer by demented hedgehog for Cleanest way to obtain the numeric prefix of a string


Simpler version (leaving the other answer as there's some interesting debate about which approach is better)

input[:-len(input.lstrip("0123456789"))]  

Answer by Marius Gedminas for Cleanest way to obtain the numeric prefix of a string


Another regexp version strips away everything starting with the first non-digit:

import re  output = re.sub('\D.*', '', input)  

Answer by Xiflado for Cleanest way to obtain the numeric prefix of a string


input = '123abc456def'  output = re.findall(r'^\d+', input)  

Will return ['123'] too.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.