filter digit number in string
2010-04-30 00:46
381 查看
http://stackoverflow.com/questions/1450897/python-removing-characters-except-digits-from-string
import re
re.sub("/D", <
4000
span class="str">"", "aas30dsa20")
'3020'
Or you can use
filter(lambda x: x.isdigit(), "aas30dsa20")
Since in Python 3k,
s=''.join(i for i in s if i.isdigit())
Another generator variant.
>>> s = "foo200bar"
>>> new_s = "".join(i for i in s if i in "0123456789")
>>> text = "9jk78k.9k87h.ji09j9oj"
>>> print "".join(i for i in text if i in ".0123456789").replace(".",",",1).replace(".","").replace(",",".")
978.987099
import re
re.sub("/D", <
4000
span class="str">"", "aas30dsa20")
'3020'
/Dmatches any non-digit character so, the code above, is essentially replacing every non-digit character for the empty string.
Or you can use
filter, like so (in Python 2k):
filter(lambda x: x.isdigit(), "aas30dsa20")
Since in Python 3k,
filterreturns an iterator instead of a
list, you can use the following instead:
>>> ''.join(filter(lambda x: x.isdigit(), "aas30dsa20")) '3020'
s=''.join(i for i in s if i.isdigit())
Another generator variant.
>>> s = "foo200bar"
>>> new_s = "".join(i for i in s if i in "0123456789")
>>> text = "9jk78k.9k87h.ji09j9oj"
>>> print "".join(i for i in text if i in ".0123456789").replace(".",",",1).replace(".","").replace(",",".")
978.987099
相关文章推荐
- 20170223-leetcode-091-Number of Segments in a String
- 434_Number_of_Segments_in_a_String
- Convert IP addresses from a dots-and-number string to a struct in_addr and back
- Number of Segments in a String
- Get number from string with regular expression in java
- Invalid number of points in LineString (found 1 - must be 0 or >= 2)
- leetcode[Number of Segments in a String]//待整理多种解法
- In MySQL, a zero number equals any string
- Number of Segments in a String
- String-434-Number of Segments in a String
- ValueError: The hardcoded shape for thenumber of rows in the filter (5) isn't the run time shape (6)
- In MySQL, a zero number equals any string
- Leetcode: Number of Segments in a String
- ORA-01704: string literal too long 和 ORA-00932: inconsistent datatypes: expected NUMBER got LONG
- string to number code in C++ template
- Leetcode Number of Segments in a String
- Number of Segments in a String
- Number of Segments in a String
- ORA-23421: job number string is not a job in the job queue
- leetcode 434- Number of Segments in a String