|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- def geshu(x):
- #我这是统计输入的一串字符中分别共有字母,数字空格和其他字符的多少
- b = list(x)
- zimu = 0
- kongge = 0
- shuzi = 0
- qita = 0
- for i in b:
- if i=='':
- kongge+=1
- elif i.isalpha():
- zimu+=1
- elif i.isdigit():
- shuzi+=1
- else :
- qita+=1
- print('传入的字符串参数中一共有%d个英文字母,%d个空格,%d个数字和%d个其他字符'% (zimu,kongge,shuzi,qita))
- x = input('请输入一串字符,统计相应参数的多少:')
- geshu(x)
复制代码
我发现用了isalpha()这个它是连中文文字也算进去的,能去掉中文文字然后统计单纯的字母吗,求解
看看我的代码吧,这个好理解点
- def countstr(*mstr):
- chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
- nums = '0123456789'
- whites = ' '
- length = len(mstr)
- for count in range(length):
- con_s = 0
- con_c = 0
- con_n = 0
- con_w = 0
- for each in mstr[count]:
- if each in chars:
- con_c += 1
- elif each in nums:
- con_n += 1
- elif each in whites:
- con_w += 1
- else:
- con_s += 1
- print('第 %d 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。' % (count + 1, con_c, con_n, con_w, con_s))
复制代码
调用这个函数即可。
|
|