python如何统计字符中有多少数字例如123abc中有3个数字
2个回答
展开全部
def count_digits(s):
return len([i for i in s if i.isdigit()])
def count_digits2(s):
return map(str.isdigit ,s).count(True)
s='a1b2c345'
print count_digits(s)
print count_digits2(s)
return len([i for i in s if i.isdigit()])
def count_digits2(s):
return map(str.isdigit ,s).count(True)
s='a1b2c345'
print count_digits(s)
print count_digits2(s)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
from string import digits
def countdigit(s):
return len(filter(lambda x: x in digits, s))
or
import re
digitpatt = re.compile('\d')
def countdigits(s):
return len(digitpatt.findall(s))
def countdigit(s):
return len(filter(lambda x: x in digits, s))
or
import re
digitpatt = re.compile('\d')
def countdigits(s):
return len(digitpatt.findall(s))
追问
要求是
def count_digits():
(str) -> int
例如
>>>count_digits(123abc456)
6
感激不尽
追答
Type "help", "copyright", "credits" or "license" for more information.
>>> from string import digits
>>> def countdigit(s):
... return len(filter(lambda x: x in digits, s))
...
>>> countdigit('123abc456')
6
>>> import re
>>> digitpatt = re.compile('\d')
>>> def countdigits(s):
... return len(digitpatt.findall(s))
...
>>> countdigit('123abc456')
6
>>>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询