Last active
April 3, 2022 09:11
-
-
Save lsauer/6088767 to your computer and use it in GitHub Desktop.
Python: JavaScript like parseInt function in one line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lsauer.com, 2013 | |
#Note: -)for convenience the function uses the re-module, but can be rewritten to fit into a lambda expression | |
# -)choose any other, more-expressive return type such as NumPy's `nan` over None if you like | |
#demonstrative-version: | |
def parseInt(sin): | |
import re | |
return int(''.join([c for c in re.split(r'[,.]',str(sin))[0] if c.isdigit()])) if re.match(r'\d+', str(sin), re.M) and not callable(sin) else None | |
#via a simple regex: | |
def parseInt(sin): | |
import re | |
m = re.search(r'^(\d+)[.,]?\d*?', str(sin)) | |
return int(m.groups()[-1]) if m and not callable(sin) else None | |
#as a lambda function: | |
parseInt = lambda sin: int(''.join([c for c in str(sin).replace(',','.').split('.')[0] if c.isdigit()])) if sum(map(int,[s.isdigit() for s in str(sin)])) and not callable(sin) and str(sin)[0].isdigit() else None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#like parseInt but allow potentially parsed numbers to begin-left with a string | |
#demonstrative-version: | |
def lparseInt(sin): | |
import re | |
return int(''.join([c for c in re.split(r'[,.]',str(sin))[0] if c.isdigit()])) if re.findall(r'(\d+)[.,]?\d*?', str(sin), re.M) and not callable(sin) else None | |
#via a simple regex: | |
def lparseInt(sin): | |
import re | |
m = re.search(r'(\d+)[.,]?\d*?', str(sin)) | |
return int(m.groups()[-1]) if m and not callable(sin) else None | |
#as a lambda function: | |
lparseInt = lambda sin: int(''.join([c for c in str(sin).replace(',','.').split('.')[0] if c.isdigit()])) if sum(map(int,[s.isdigit() for s in str(sin)])) and not callable(sin) else None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#most useful in combination with map, filter and reduce | |
map(parseInt, ['', 100, '234324']) #... | |
#> [None, 100, 234324]... | |
#tests | |
parseInt(None) | |
parseInt(lambda: None) | |
parseInt('') | |
parseInt(r'') | |
parseInt(100) | |
parseInt(()) | |
parseInt((0,1)) | |
parseInt([]) | |
parseInt(100.23) | |
parseInt('abcd100,10') | |
parseInt('100,30') | |
parseInt('100.30') | |
parseInt('abcd100,10dddd') | |
parseInt('abcd100.10dddd') | |
parseInt('100,10dddd') | |
parseInt('100.10dddd') | |
parseInt('1234567890.1234567890') | |
parseInt('1234567890,1234567890') | |
parseInt('123456789012345678901234567890123456789012345678901234567890') | |
_tarr = [19, | |
None, | |
lambda: None, | |
'', | |
'', | |
100, | |
(), | |
(0, 1), | |
[], | |
100.23, | |
'abcd100,10', | |
'100,30', | |
'100.30', | |
'abcd100,10dddd', | |
'abcd100.10dddd', | |
'100,10dddd', | |
'100.10dddd', | |
'1234567890.1234567890', | |
'1234567890,1234567890', | |
'123456789012345678901234567890123456789012345678901234567890'] | |
#results: | |
In[1]: zip(_tarr,map(parseInt,_tarr)) | |
Out[1]: | |
[(19, 19), | |
(None, None), | |
(<function __main__.<lambda>>, None), | |
('', None), | |
('', None), | |
(100, 100), | |
((), None), | |
((0, 1), None), | |
([], None), | |
(100.23, 100), | |
('abcd100,10', None), | |
('100,30', 100), | |
('100.30', 100), | |
('abcd100,10dddd', None), | |
('abcd100.10dddd', None), | |
('100,10dddd', 100), | |
('100.10dddd', 100), | |
('1234567890.1234567890', 1234567890), | |
('1234567890,1234567890', 1234567890), | |
('123456789012345678901234567890123456789012345678901234567890', | |
123456789012345678901234567890123456789012345678901234567890L)] | |
In [2]:zip(_tmp,map(lparseInt,_tmp)) | |
Out[2]: | |
[(19, 19), | |
(None, None), | |
(<function __main__.<lambda>>, None), | |
('', None), | |
('', None), | |
(100, 100), | |
((), None), | |
((0, 1), 0), #attention! | |
([], None), | |
(100.23, 100), | |
('abcd100,10', 100), | |
('100,30', 100), | |
('100.30', 100), | |
('abcd100,10dddd', 100), | |
('abcd100.10dddd', 100), | |
('100,10dddd', 100), | |
('100.10dddd', 100), | |
('1234567890.1234567890', 1234567890), | |
('1234567890,1234567890', 1234567890), | |
('123456789012345678901234567890123456789012345678901234567890', | |
123456789012345678901234567890123456789012345678901234567890L)] |
@fanglijun
you should try this:
def parse_int(x): temp = [] for c in str(x).strip(): if c.isdigit(): temp.append(c) else: break return int("".join(temp)) if "".join(temp) else None
@karthikeyan5 thanks, that's precisely what I needed. Thanks! Post this on stackoverflow!
or simpler:
def parseInt(s):
digits = ''
for c in str(s).strip():
if not c.isdigit():
break
digits += c
return int(digits) if digits else None
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result: 223
Is this corrected?
javascript would output 2